PyArray_FROM_OF from Cython
I'm trying to use PyArray_FROM_OF from Cython and the generated C code keeps crashing. Dag said on the Cython list that he wasn't sure what was going on, so maybe someone here will have an idea. The line that gdb says is crashing is: #0 0x00e48287 in __pyx_pf_3_vq_vq (__pyx_self=0x0, __pyx_args=0xca2d8, __pyx_kwds=0x0) at _vq_rewrite.c:1025 1025 __pyx_t_1 = PyArray_FROM_OF(((PyObject *)__pyx_v_obs), __pyx_v_flags); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} obs and obs_a are both cdef'd np.ndarrays, and the former (obs) is passed in as an argument. I define flags as cdef int flags = np.NPY_CONTIGUOUS | np.NPY_ALIGNED | np.NPY_NOTSWAPPED and then the line that crashes is obs_a = np.PyArray_FROM_OF(obs, flags) Does anyone know what I'm doing wrong? (I know I could use np.ascontiguous, but as far as I can tell this _should_ work) David
On Thu, Oct 8, 2009 at 17:28, David Warde-Farley
I'm trying to use PyArray_FROM_OF from Cython and the generated C code keeps crashing. Dag said on the Cython list that he wasn't sure what was going on, so maybe someone here will have an idea.
You must call import_array() at the top level before you can use any numpy C API functions. http://wiki.cython.org/tutorials/numpy#UsingtheNumpyCAPI -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On 8-Oct-09, at 6:47 PM, Robert Kern wrote:
On Thu, Oct 8, 2009 at 17:28, David Warde-Farley
wrote: I'm trying to use PyArray_FROM_OF from Cython and the generated C code keeps crashing. Dag said on the Cython list that he wasn't sure what was going on, so maybe someone here will have an idea.
You must call import_array() at the top level before you can use any numpy C API functions.
Thanks. One more thing: calling Py_DECREF on arrays that I have acquired from PyArray_FROM_OF seems to cause crashes, am I correct in assuming that Cython is somehow tracking all the PyObjects in the scope (even ones acquired via the NumPy C API) and DECREF'ing it for me? David
On Thu, Oct 8, 2009 at 19:32, David Warde-Farley
On 8-Oct-09, at 6:47 PM, Robert Kern wrote:
On Thu, Oct 8, 2009 at 17:28, David Warde-Farley
wrote: I'm trying to use PyArray_FROM_OF from Cython and the generated C code keeps crashing. Dag said on the Cython list that he wasn't sure what was going on, so maybe someone here will have an idea.
You must call import_array() at the top level before you can use any numpy C API functions.
Thanks. One more thing: calling Py_DECREF on arrays that I have acquired from PyArray_FROM_OF seems to cause crashes, am I correct in assuming that Cython is somehow tracking all the PyObjects in the scope (even ones acquired via the NumPy C API) and DECREF'ing it for me?
It usually does, yes. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
David Warde-Farley wrote:
On 8-Oct-09, at 6:47 PM, Robert Kern wrote:
On Thu, Oct 8, 2009 at 17:28, David Warde-Farley
wrote: I'm trying to use PyArray_FROM_OF from Cython and the generated C code keeps crashing. Dag said on the Cython list that he wasn't sure what was going on, so maybe someone here will have an idea. You must call import_array() at the top level before you can use any numpy C API functions.
Thanks. One more thing: calling Py_DECREF on arrays that I have acquired from PyArray_FROM_OF seems to cause crashes, am I correct in assuming that Cython is somehow tracking all the PyObjects in the scope (even ones acquired via the NumPy C API) and DECREF'ing it for me?
If the function is declared with "object" as return type (or nothing which defaults to the same thing), then Cython will interpret that as the function handing away the reference of the object and make sure it is decref-ed. -- Dag Sverre
participants (3)
-
Dag Sverre Seljebotn
-
David Warde-Farley
-
Robert Kern