AFAIK, you only have to INCREF the dtype. And only for numpy array
creation routines that take a dtype object as an argument (they
"steal" a dtype reference, which is why you have to incref it).


On Sun, Nov 15, 2009 at 2:09 AM, Matthew Brett <matthew.br...@gmail.com> wrote:
> Hi,
>
> I've been spending some time with Cython in Scipy:
> http://projects.scipy.org/scipy/browser/trunk/scipy/io/matlab
>
> In general, it's been very enjoyable - a warm thank you to y'all for
> making it so clear and so useful.
>
> Here, I just wanted to check that I had understood the idioms right to
> call the numpy C-API.  I'm doing this for speed.
>
> Imagine I have a string 'data', length 'n' bytes, and a numpy dtype
> 'dt', and I wanted to make a 1d array.  In python that is:
>
> arr = np.ndarray(shape=(n,), dtype=dt, buffer=data)
>
> Am I right, that in Cython, this would be a function like the
> following (that does work):
>
> # start of make_array.pyx
> from python cimport Py_INCREF
>
> cdef extern from "Python.h":
>    ctypedef struct PyTypeObject:
>        pass
>
> import numpy as np
> cimport numpy as cnp
>
> cdef extern from "numpy/arrayobject.h":
>    PyTypeObject PyArray_Type
>    object PyArray_NewFromDescr(PyTypeObject *subtype,
>                                cnp.dtype newdtype,
>                                int nd,
>                                cnp.npy_intp* dims,
>                                cnp.npy_intp* strides,
>                                void* data,
>                                int flags,
>                                object parent)
>
> cdef extern from "workaround.h":
>    void PyArray_Set_BASE(cnp.ndarray arr, object obj)
>
> # NOTE: numpy MUST be initialized before any other code is executed.
> cnp.import_array()
>
>
> def make_1d_array(cnp.npy_intp size, object data, cnp.dtype dt):
>    cdef char *ptr
>    ptr = data
>    Py_INCREF(<object> dt)
>    Py_INCREF(<object> data)
>    narr = PyArray_NewFromDescr(&PyArray_Type,
>                                 dt,
>                                 1,
>                                 &size,
>                                 NULL,
>                                 <void *>ptr,
>                                 0,
>                                 <object>NULL)
>    PyArray_Set_BASE(narr, data)
>    return narr
> # end of make_array.pyx
>
> where 'workaround.h' (thanks to Dag Sverre) [1]:
>
> #include <numpy/arrayobject.h>
> #define PyArray_Set_BASE(arr, obj) PyArray_BASE(arr) = obj
>
> Is that correct?  In particular, I don't think I fully understand the
> right idiom for using the numpy array type PyArray_Type.  Did I
> understand correctly that I have to Py_INCREF both the dtype and the
> data? [2] [3]
>
> Many thanks for any help.
>
> Matthew
>
> [1] http://codespeak.net/pipermail/cython-dev/2009-June/005979.html
> [2] http://docs.scipy.org/doc/numpy/reference/c-api.array.html
> [3] http://wiki.cython.org/tutorials/numpy#UsingtheNumpyCAPI
> _______________________________________________
> Cython-dev mailing list
> Cython-dev@codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev
>
_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to