Storing a C pointer in a Python class instance

lallous lallous at lgwm.org
Wed Sep 30 08:24:38 EDT 2009


Hello

After using the PyCObject, I cannot pickle the class anymore.
Any simple solution to this problem? (or resorting to __reduce__ is the only 
solution?)

Thanks,
Elias

"Falcolas" <garrickp at gmail.com> wrote in message 
news:9d3790aa-f7d9-4bb5-a81f-5428b2d607eb at v25g2000yqk.googlegroups.com...
> On Sep 29, 2:27 am, "lallous" <lall... at lgwm.org> wrote:
>> Hello
>>
>> From my C extension module I want to store a C pointer in a given 
>> PyObject.
>>
>> The only way I figure how to do it is to use Py_BuildValues and store the
>> poiner casted to Py_ssize_t, thus:
>>
>> Py_BuildValues("n", (Py_ssize_t)my_ptr)
>>
>> Can it be done differently?
>>
>> Regards,
>> Elias
>
> You can use a "PyCObject_FromVoidPtr"
>
> http://docs.python.org/c-api/cobject.html
>
> PyArg_ParseTuple(args, "O", &pyVoidPointer);
> castPointer = (type *) PyCObject_AsVoidPtr(pyVoidPointer);
> return PyCObject_FromVoidPtr((void *) castPointer, NULL); 




More information about the Python-list mailing list