[Pyrex] Re: Is this example correct?

Greg Ewing greg at cosc.canterbury.ac.nz
Mon May 12 02:27:51 CEST 2003


> Is there any pitfall to turn this into the following form, where no
> specific allocation is required:
>
>     def __new__(self, i=0, x=0.0):
>         # Removed [m]allocation of 't'
>
>     def ToPointer(self):
>         # Use '&' on t
>         return  PyCObject_FromVoidPtrAndDesc(&(self.t), 'type Num*', NULL)

There's one potential pitfall I can see -- the pointer
held by the PyCObject returned by ToPointer could become
invalid if the original object is deallocated before the
pointer gets used.

Whether this is actually a problem depends on how careful
the code that uses the PyCObject is. If it makes sure
that a reference to the original object is kept for as
long as the Num pointer is needed, things should be
okay.

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






More information about the Pyrex mailing list