[Pyrex] Subclassing numpy.ndarray with Pyrex

Lenard Lindstrom len-l at telus.net
Tue Feb 6 19:30:22 UTC 2007


Greg Ewing wrote:
> Pierre GM wrote:
>   
>> When a sub-type is created in such a fashion, 
>> however, neither the new method nor the init methods gets called.
>>     
>
> Urk, this doesn't bode well for being able to subclass
> ndarray in Pyrex. If the normal instance creation
> mechanisms are bypassed, various things that Pyrex
> relies on won't happen, such as initialisation of
> Python-valued attributes. This could be the reason
> you're getting crashes in odd places.
>
> Things should still work if you do create an instance
> in the normal way, however, so I'm not sure exactly
> what's going on.
>
> Do you really need to subclass ndarray? Could you
> get what you want using a different class that
> wraps an ndarray instead?
>
>   
I don't think numpy  was well tested with Pyrex. The provided 
c_numpy.pxd causes a pyrex compiler error involving an array of objects. 
As for the segment faults even stripping away everything but the class 
declaration doesn't stop them.

Here is the stripped Pyrex module subnd.pyx:

cimport c_python
from c_python cimport Py_intptr_t
from c_numpy cimport ndarray, npy_intp, dtype, import_array
import numpy as _N
# NumPy must be initialized
import_array()

cdef class Sub(ndarray):
    pass


The following interactive session causes a page fault in the Python DLL:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from subnd import Sub
 >>> x=Sub((4,))
 >>> del x


If I am reading the error details and the dependency walker information 
correctly then the page fault happens in PyObject_GC_Del. That's the 
best I can do without a debug version of Python.

-- 
Lenard Lindstrom
<len-l at telus.net>





More information about the Pyrex mailing list