[C++-sig] How to expose virtual function with default arguments in boost.python ?
Baptiste Lepilleur
gaiacrtn at free.fr
Sat Dec 4 16:07:36 CET 2004
I'm trying to fix a bug in Pyste regarding exposure of virtual function with
default arguments, but I can't figure out what is the correct code to
generate (e.g. this is a question regarding boost.python, not pyste).
I'm basically trying to expose the virtual member function 'set' of the
following code:
----
class VirtualNonCopyable {
public:
virtual void set( const std::string &name_, int size_ = 10, int width_ =
12 ) {
name = name_;
size = size_;
width = width_;
}
std::string name;
int size;
int width;
};
inline VirtualNonCopyable &getVirtualNonCopyable() {
static VirtualNonCopyable instance;
return instance;
}
----
Pyste generate the following binding code (full source attached):
----
class_< VirtualNonCopyable, py::VirtualNonCopyable_Wrapper
>("VirtualNonCopyable", init< >())
.def("set", &VirtualNonCopyable::set,
&py::VirtualNonCopyable_Wrapper::default_set_3)
.def("set", &py::VirtualNonCopyable_Wrapper::default_set_1)
.def("set", &py::VirtualNonCopyable_Wrapper::default_set_2)
.def(init< const VirtualNonCopyable& >())
.def_readwrite("name", &VirtualNonCopyable::name)
.def_readwrite("size", &VirtualNonCopyable::size)
.def_readwrite("width", &VirtualNonCopyable::width)
;
----
Pyste is still using old-style polymorphism. default_set_1, default_set_2
and default_set_3 are methods forwarding to the base class implementation.
The binding for default_set_1 should be the one triggered by the python code
below.
The bug is demonstrated by the following python code:
object = getVirtualNonCopyable() # Get a instance of VirtualNonCopyable
from C++
object.set( 'abc' ) # use overloading resolution
to apply the default values
Which produces the following error:
object.set( 'abc' )
Boost.Python.ArgumentError: Python argument types in
VirtualNonCopyable.set(VirtualNonCopyable, str)
did not match C++ signature:
set(struct py::VirtualNonCopyable_Wrapper {lvalue}, class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >, int)
set(struct py::VirtualNonCopyable_Wrapper {lvalue}, class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >)
set(struct py::VirtualNonCopyable_Wrapper {lvalue}, class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >, int, int)
set(class VirtualNonCopyable {lvalue}, class
std::basic_string<char,struct std::char_traits<char>,class std::allocat
or<char> >, int, int)
The issue only occurs when the object is returned from C++ and all the
parameters are not specified. The following python code works correctly:
object = getVirtualNonCopyable()
object.set( 'abc', 1, 2 ) # works with all parameters
specified
object = VirtualNonCopyable()
object.set( 'abc' ) # works on instance
created in python
I couldn't find any example of combining default argument handling with
default virtual method implementation. Does anyone know how to do so ?
Baptiste.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: main.cpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20041204/eaf38169/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: TestOverload.cpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20041204/eaf38169/attachment-0001.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: TestOverload.h
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20041204/eaf38169/attachment-0002.txt>
More information about the Cplusplus-sig
mailing list