[C++-sig] Convert C++ Class to exposed class and back

Marcus Jannes jannes80 at hotmail.de
Thu Feb 14 17:07:48 CET 2008

I got it working with my previous C API approach, there were just some bugs like not passing a tuple to the PyEval_CallObject function. Indeed the function pointer does not need to be stored for further use. I might try your suggestion at a later point too, thanks for your help, much appreciated. I think the Boost Python FAQ could be extended in this matter, it isn't that clear or its my lack of english ;-)

> Again, it can't be done any less (or more) in boost.python than it can 
> be done with the C API. You still face the same question (which David 
> points out in the FAQ, as you noted): you need to pass some state that 
> needs to be remembered, close to the function (pointer).
> If the execution of that function is synchronous, i.e. if your C++ 
> library does not intend to store the function pointer and call it later, 
> you may get away by setting some static data during the upcall.
> However, this won't work if the execution is deferred, as you may call 
> fEval from within python multiple times, each time overriding the 
> closure data set in the previous call.
> Here is how it *might* work:
> // remember the current python function
> bp::object current_callable;
> // implement suitable ddf_FctPtr that executes
> // the current python function
> DerivType call(DerivType const &argument)
> {
>    bp::object retn = current_callable(argument);
>    return bp::extract(retn);
> }
> // The function to be called in Python. The return value is returned 
> directly, not passed back by-reference.
> DerivType py_fEval(bp::object function, DerivType const &argument)
> {
>    DerivType retn;
>    current_callable = function;
>    // assume 'call' won't be stored beyond this call, and so we can
>    // be sure 'current_callable' doesn't change its value.
>    fEval(call, argument, retn);
>    return retn;
> }
> Now you only need to export 'py_fEval' as 'fEval' to python and you 
> should be all set. Remember: this trick only works if the function 
> pointer won't be retained and called asynchronously, as then 
> 'current_callable' may long have assumed a different value.
> HTH,
> 		Stefan
> -- 
>        ...ich hab' noch einen Koffer in Berlin...
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig

Neu: Internet Explorer 7 optimiert für MSN!

More information about the Cplusplus-sig mailing list