[C++-sig] Pyste: set_wrapper on overloaded function or method
Dan Halbert
halbert at jubilee.bbn.com
Sun Mar 14 18:03:34 CET 2004
I was trying to create a wrapper for an overloaded method using the
Pyste "set_wrapper" capability (pyste from Boost 1.31.0). However, I ran
into a problem which can be illustrated by the following example:
========================================
testing.pyste:
--------------
testing = AllFromHeader("testing.h")
set_wrapper(testing.f, "f_wrapper")
set_wrapper(testing.C.g, "g_wrapper")
========================================
testing.h:
----------
int f(int i=1) { return i;}
struct C {
int g(int i=2) { return i;}
};
int f_wrapper(float fl=3.0) { return (int) fl; }
int g_wrapper(C* c, float fl=4.0) { return (int) fl; }
========================================
Pyste produces a .cpp which includes this code:
//...
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(C_g_overloads_0_1, g, 0, 1)
BOOST_PYTHON_FUNCTION_OVERLOADS(f_overloads_0_1, f, 0, 1)
BOOST_PYTHON_FUNCTION_OVERLOADS(f_wrapper_overloads_0_1, f_wrapper, 0, 1)
BOOST_PYTHON_FUNCTION_OVERLOADS(g_wrapper_overloads_1_2, g_wrapper, 1, 2)
// ...
BOOST_PYTHON_MODULE(testing)
{
class_< C >("C", init< >())
.def(init< const C& >())
.def("g", &g_wrapper, C_g_overloads_0_1()) // ** Line g **
;
def("f", &f_wrapper, f_overloads_0_1()); // ** Line f **
// ...
}
========================================
The problem is that in Lines g and f above, the third args to ".def"
are the wrong overloads to use. They are the overloads for the
original method or function, not the wrapper overloads. The code
should be:
.def("g", &g_wrapper, g_wrapper_overloads_1_2()) // corrected Line g
def("f", &f_wrapper, f_wrapper_overloads_0_1()); // corrected Line f
If f_wrapper and g_wrapper had no overloads, there should be no third
arg at all.
I tried to fix this by patching ClassExporter.py around line 376,
where the third arg to ".def" is constructed. I checked for a wrapper
and tried to generate the overloads arg for the wrapper, not the
method. But I was stymied because the wrapper object is not a
full-fledged declaration object, and I can't get its min and max args.
I'm still looking at the code, but perhaps someone more familar with
it could suggest a fix.
The same kind of fix would need to go into FunctionExporter.py.
(In the example above, I would normally have done an
Include("testing_wrapper.h"). I left it out for simplicity and also to
force the BOOST_PYTHON_FUNCTION_OVERLOADS to occur.)
Regards,
Dan
More information about the Cplusplus-sig
mailing list