[Numpy-discussion] subclassing array in c

mark florisson markflorisson88 at gmail.com
Fri Mar 30 16:38:11 EDT 2012


On 30 March 2012 19:53, Chris Barker <chris.barker at noaa.gov> wrote:
> On Fri, Mar 30, 2012 at 10:57 AM, mark florisson
> <markflorisson88 at gmail.com> wrote:
>> Although the segfault was caused by a bug in NumPy, you should
>> probably also consider using Cython, which can make a lot of this pain
>> and boring stuff go away.
>
> Is there a good demo/sample somewhere of an ndarray subclass in Cython?
>
> Some quick googling turned up a number of people asking about it, but
> I didn't find (quickly) a wiki page or demo about it.
>
> -Chris
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

It's not common to do, I tried the following:

cimport numpy

cdef extern from "Python.h":
    ctypedef struct PyTypeObject:
        void *tp_alloc

    object PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)

cdef myalloc(PyTypeObject *type, Py_ssize_t nitems):
    print "allocating"
    return PyType_GenericAlloc(type, nitems)

cdef class MyClass(numpy.ndarray) :
    cdef int array[10000000]

(<PyTypeObject *> MyClass).tp_alloc = <void *> myalloc # This works
around the NumPy bug
cdef MyClass obj = MyClass((10,))
obj.array[999999] = 20

The array attribute is quite large here to cause a segfault if our
trick to replace the tp_alloc isn't working. It's kind of a hack, but
the only alternative is to use composition instead.



More information about the NumPy-Discussion mailing list