[Pyrex] Re: Is this example correct?

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Fri May 9 16:03:08 CEST 2003


On Mon, 05 May 2003 22:18:15 +0000, Edward C. Jones wrote:

> Is the code below correct? Can I reduce the number of calls to malloc? 
> Any ideas for improvement?
>
 
[...code snipped...]

Sorry for this late question, but I am not familiar with the extension
types and I may be missing something.

The examples provided by Edward perform allocation/deallocation at
initialisation/destruction of the object. 

Is there any pitfall to turn this into the following form, where no
specific allocation is required:

cdef class TemP__Num:
     # --> replace 't' as a pointer to Num into a full Num
     cdef Num t

     def __new__(self, i=0, x=0.0):
         # Removed [m]allocation of 't'
         if i != 0 and x != 0.0:
             raise TypeError, 'both i and x are non-zero'
         if x != 0.0:
             self.t.x = x
         else:
             self.t.i = i

     def __dealloc__(self):
         # Removed freeing 't'
         pass

     ...

     def ToPointer(self):
         # Use '&' on t
         return  PyCObject_FromVoidPtrAndDesc(&(self.t), 'type Num*', NULL)

-- 
Pedro





More information about the Pyrex mailing list