[Pyrex] public extension type to replace a complex structure variable

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Feb 22 02:58:36 CET 2005


Gavin Baker wrote:
> I have come across this initialisation problem also, whereby you often
> want to create a Python object to encapsulate an existing struct so
> you need to create it empty and use a cdef method to give it a
> pointer.  Apart from asserting the pointer is not NULL in every
> method, I'm not sure how best to handle this.
> 
> Any further light you can shed on this would be very useful, so I'd
> certainly appreciate some more details when you get a chance.

Possible approaches include:

* As you mentioned, check for a null C pointer whenever
you use it. Not very desirable -- tedious, error prone
and inefficient.

* Keep a dummy C object around to use as an initial
value for "empty" object wrappers (taking care never to
deallocate it).

* Pass a C pointer into the constructor wrapped in a
PyCObject, and have the constructor check the PyCObject's
identifying string to make sure it looks like the right
kind of PyCObject.

This is the most elegant solution I've been
able to think of so far. It should be completely safe
from Python code, since pure Python can't create a
PyCObject, and it's unlikely that another extension
module would accidentally create a PyCObject with the
same identifying string if it's sensibly chosen.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Pyrex mailing list