cython

changeset 1327:b072eae5f5a9

numpy.pxd support also when Py_ssize_t and npy_intp do not have the same size.
author David Cournapeau <david@ar.media.kyoto-u.ac.jp>
date Mon Nov 10 13:19:04 2008 +0100 (3 years ago)
parents 764f1578df40
children 5278c13c1db6
files Cython/Includes/numpy.pxd
line diff
1.1 --- a/Cython/Includes/numpy.pxd Sat Nov 08 17:49:58 2008 -0800 1.2 +++ b/Cython/Includes/numpy.pxd Mon Nov 10 13:19:04 2008 +0100 1.3 @@ -52,8 +52,11 @@ 1.4 # requirements, and does not yet fullfill the PEP. 1.5 # In particular strided access is always provided regardless 1.6 # of flags 1.7 + cdef int copy_shape 1.8 if sizeof(npy_intp) != sizeof(Py_ssize_t): 1.9 - raise RuntimeError("Py_intptr_t and Py_ssize_t differs in size, numpy.pxd does not support this") 1.10 + copy_shape = 1 1.11 + else: 1.12 + copy_shape = 0 1.13 1.14 if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS) 1.15 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)): 1.16 @@ -65,8 +68,15 @@ 1.17 1.18 info.buf = PyArray_DATA(self) 1.19 info.ndim = PyArray_NDIM(self) 1.20 - info.strides = <Py_ssize_t*>PyArray_STRIDES(self) 1.21 - info.shape = <Py_ssize_t*>PyArray_DIMS(self) 1.22 + if copy_shape: 1.23 + info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * info.ndim) 1.24 + info.shape = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * info.ndim) 1.25 + for i in range(info.ndim): 1.26 + info.strides[i] = PyArray_STRIDES(self)[i] 1.27 + info.shape[i] = PyArray_DIMS(self)[i] 1.28 + else: 1.29 + info.strides = <Py_ssize_t*>PyArray_STRIDES(self) 1.30 + info.shape = <Py_ssize_t*>PyArray_DIMS(self) 1.31 info.suboffsets = NULL 1.32 info.itemsize = PyArray_ITEMSIZE(self) 1.33 info.readonly = not PyArray_ISWRITEABLE(self) 1.34 @@ -86,9 +96,14 @@ 1.35 # (this would look much prettier if we could use utility 1.36 # functions). 1.37 1.38 - 1.39 + if not hasfields and not copy_shape: 1.40 + # do not call releasebuffer 1.41 + info.obj = None 1.42 + else: 1.43 + # need to call releasebuffer 1.44 + info.obj = self 1.45 + 1.46 if not hasfields: 1.47 - info.obj = None # do not call releasebuffer 1.48 t = descr.type_num 1.49 if t == NPY_BYTE: f = "b" 1.50 elif t == NPY_UBYTE: f = "B" 1.51 @@ -112,7 +127,6 @@ 1.52 info.format = f 1.53 return 1.54 else: 1.55 - info.obj = self # need to call releasebuffer 1.56 info.format = <char*>stdlib.malloc(255) # static size 1.57 f = info.format 1.58 stack = [iter(descr.fields.iteritems())] 1.59 @@ -170,6 +184,9 @@ 1.60 # This can not be called unless format needs to be freed (as 1.61 # obj is set to NULL in those case) 1.62 stdlib.free(info.format) 1.63 + if sizeof(npy_intp) != sizeof(Py_ssize_t): 1.64 + stdlib.free(info.shape) 1.65 + stdlib.free(info.strides) 1.66 1.67 1.68 cdef void* PyArray_DATA(ndarray arr)