[C++-sig] Converting C++ Class into Python Binding

Clay Culver clay at idleengineer.net
Wed Feb 16 19:00:07 CET 2005


Ralf W. Grosse-Kunstleve wrote:

>
>
>I am not competent enough to give you a definitive answer, but I believe this
>should get you close:
>
>boost::python::object
>get_foo_object(...)
>{
>  ...
>  return boost::python::object(bar);
>}
>
>I don't know how to control pass-by-reference vs. pass-by-value. I'd look for
>"boost::ref" in the tutorial since this is the best clue I have off the top of
>my head.
>  
>
This is what I've seen before, but I just need to be sure that a copy of 
the object is not made (which is usually the case in python).  If I get 
this working I can just test it.

>Also, this is unusual in a Boost.Python wrapper:
> 
>  
>
>>get_foo_object( PyObject *self, PyObject *args )
>>    
>>
>
>Why don't you use the "native" signature instead?
>  
>
This is how you would do it with the Python/C api.  I seem to be having 
difficulty making the transition to boost.  I have a decent grasp of how 
to wrap C++ objects objects into a python object, but what's more fuzzy 
is working with the interpreter using boost.  I'm trying to embed Python 
into my application, but I get all the way to the point where I need to 
start returning references to C++ objects and I get stuck.  My main flow is:

0) I already have library Foo bound in both Python and C++ (import Foo 
works in python, in C++ I include Foo headers).
1) Build a new module for this application to expose the data I'm trying 
to share.  (get_foo_object would be one of the bound methods so that 
python could call "import new_module", "f = new_module.get_foo_object()" 
and from there start manipulating f)
2) Startup the interpreter.
3) Load the module I created in 1 with Py_InitModule.
4) Use python scripts to call methods in this new module to interact 
with the data.

The only problem I seem to be having is during #1, trying to convert 
from a C++ object to its wrapper.  I'll try return 
boost::python::object(bar).

Am I going about this the wrong way?  Should I be using pure boost 
instead of trying to do this with the Python/C API?

Thanks,
Clay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050216/a3e0413d/attachment.htm>


More information about the Cplusplus-sig mailing list