[Numpy-discussion] subclassing array in c
Fri Mar 30 15:40:09 CDT 2012
On 30 March 2012 21:38, mark florisson <email@example.com> wrote:
> On 30 March 2012 19:53, Chris Barker <firstname.lastname@example.org> wrote:
>> On Fri, Mar 30, 2012 at 10:57 AM, mark florisson
>> <email@example.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.
>> Christopher Barker, Ph.D.
>> 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
>> NumPy-Discussion mailing list
> 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
> (<PyTypeObject *> MyClass).tp_alloc = <void *> myalloc # This works
> around the NumPy bug
> cdef MyClass obj = MyClass((10,))
> obj.array = 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.
(So remove the array attribute, it's just for demonstration :)
More information about the NumPy-Discussion