how to pass C++ object to another C++ function via Python function

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Apr 21 19:54:29 EDT 2008


En Mon, 21 Apr 2008 19:11:31 -0300, grbgooglefan <ganeshborse at gmail.com>  
escribió:
> On Apr 21, 10:17 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>> En Mon, 21 Apr 2008 10:24:15 -0300, grbgooglefan  
>> <ganeshbo... at gmail.com> escribió:
>>
>> > I am trying to pass a C++ object to Python function. This Python
>> > function then calls another C++ function which then uses this C++
>> > object to call methods of that object's class.
>>
>> > I tried something like this, but it did not work, gave core dump.
>>
>> You can't pass any arbitrary C object to a Python function.
>> In this case you can use a PyCObject, a Python box around a void*  
>> pointer.
>> Seehttp://docs.python.org/api/cObjects.html
>
> Yup, I looked at http://www.python.org/doc/ext/using-cobjects.html
> also. I could not find in this example where is CObject used or
> converted back from Python to C.
> Is there any tutorial which I can use for this?

If you have a C function that receives a PyCObject, just include the  
relevant headers (cobject.h) and you can retrieve the original pointer  
using PyCObject_AsVoidPtr:

void foo(PyObject *pyobj)
{
   TOriginalType *porig;
   porig = (TOriginalType *)PyCObject_AsVoidPtr(pyobj);
   // do something with porig
}

-- 
Gabriel Genellina




More information about the Python-list mailing list