question about handling pointers in C extensions

chris liechti cliechti at mails.ch
Tue Jul 24 17:45:18 EDT 2001


seams reasonable for me. bt not that i have worked with python and c 
pointers. i've done this by making a python extension module that provides 
access to the device functions (were some GUI methonds).

for me the cleanest thing would be writing a c extension module that 
provides a python class. this class has methods to communicate with the 
driver (thats no problem as it is written in c). that way you don's have to 
expose any pointers to the python code and you dont have to worry about 
memory leaks as a python object gets always finalized if no longer needed. 
just put your cleanup code in that classes __del__ method.

here some exaple sourse as it would look for your approach:
 import mydriver
 handle = mydriver.open()
 mydriver.dosomething(handle, args)
 mydriver.close(handle)

the above description gives you something like this:
 import mydriver
 dev = mydriver.open()
 dev.dosomething(args)
 dev.close()

no "handle" carriend around - a nice obejct oriented solution.

chris



"Russell E. Owen" <owen at astrono.junkwashington.emu> wrote in 
news:9jkhdv$r3o$1 at nntp6.u.washington.edu:

> I would like a bit of help with handling pointers in C extensions. I 
> hope this is a simple question.
> 
> I want to write a Python interface to a device driver library. One 
> starts by opening the device and getting a particular kind of pointer, 
> e.g. (simplified):
>   DevType *dev_open()
> 
> Is the following reasonable, or is there a better way to do this?
> 
> static PyObject *open (PyObject *self, PyObject *args)
> ) {
>     PyObject *py_dev_p;
>     DevType *dev_p;
>     
>     /* call the C routine to open the device */
>     dev_p = dev_open();
>     if (dev_p == NULL) {
>         PyErr_SetString(PyExc_RuntimeError, "open failed");
>         return NULL;
>     }
> 
>     /* convert device pointer to a Python Long object */
>     py_dev_p = PyLong_FromVoidPtr ((void *) dev_p);
>     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);
> }
> 
> Also, are there any tricks or gotchas to passing the pointer in to 
> device control functions?
> 
> I am assuming the inverse functions are the ticket (parse "O" format to 
> extract the PyLong, then use PyLong_AsVoidPtr). "O!" sounded interesting 
> to get a void* in one step, but what are the "Python type objects" one 
> can pass? Anyone have a simple example of a converter function for "O%"?
> 
> I assume:
> - there is no need to INCREF or DECREF the PyLong pointer (so long as my 
> extension only uses it locally, i.e. doesn't store it)
> - eventually the user should call a Python interface to dev_close and at 
> that time the PyLong should be DECREFed.
> 
> Any advice or warnings would be appreciated. I have been reading 
> "Extending and Embedding" and "Python/C API", but I still have doubts 
> I've got the pointer handling right.
> 
> Regards,
> 
> -- Russell
> 



-- 
chris <cliechti at mails.ch>




More information about the Python-list mailing list