question about handling pointers in C extensions

Russell E. Owen owen at astrono.junkwashington.emu
Wed Jul 25 11:59:17 EDT 2001


In article <yv2lmld4fhg.fsf at lionsp093.lion-ag.de>,
 Harald Kirsch <kirschh at lionbioscience.com> wrote:

>>     /* convert device pointer to a Python Long object */
>>     py_dev_p = PyLong_FromVoidPtr ((void *) dev_p);
>
>This makes py_dev_p into a reference you own, therefore ...
>
>>     if (py_dev_p == NULL) {
>>         return NULL;
>>     }
>> 
>>     /* increment reference and return the PyLong */
>>     Py_INCREF (py_dev_p);
>>     return Py_BuildValue("O", py_dev_p);
>
>... neither INCREF nor BuildValue are needed.

Thanks for the save!!!

You and Chris Liechti both then go on to suggest creating a new Python 
type. I'm a bit confused here. This would create a Python class or an 
actual new Python data type? "Python Essential Reference" makes creating 
a new data type scary, but I haven't been able to figure out if that 
applies equally to creating a Python object.

Anyway, I'd love to convert my C interface to one that produces Python 
objects. The code would be much cleaner to use in Python. I'll have 
another look at it. If anybody has a pointer to simple sample code or 
documentation beyond "Extending and Embedding" and "Python/C API", 
please let me know. I figure I'll start with straight function calls 
(and associated icky pointers), get that working, then see if I can 
create Python objects.

-- Russell



More information about the Python-list mailing list