Cython has moved to github.
cython
view Cython/Includes/numpy.pxd @ 1330:991df2fe12e6
Fix segfault problem with recent numpy.pxd/Python 2.4 patch
| author | Dag Sverre Seljebotn <dagss@student.matnat.uio.no> |
|---|---|
| date | Mon Nov 10 14:13:36 2008 +0100 (3 years ago) |
| parents | e8388b451d86 |
| children | fa14f80b335c |
line source
1 cimport python_buffer as pybuf
2 cimport stdlib
4 cdef extern from "Python.h":
5 ctypedef int Py_intptr_t
7 cdef extern from "numpy/arrayobject.h":
8 ctypedef Py_intptr_t npy_intp
10 cdef enum:
11 NPY_BOOL,
12 NPY_BYTE, NPY_UBYTE,
13 NPY_SHORT, NPY_USHORT,
14 NPY_INT, NPY_UINT,
15 NPY_LONG, NPY_ULONG,
16 NPY_LONGLONG, NPY_ULONGLONG,
17 NPY_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE,
18 NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE,
19 NPY_OBJECT,
20 NPY_STRING, NPY_UNICODE,
21 NPY_VOID,
22 NPY_NTYPES,
23 NPY_NOTYPE,
24 NPY_CHAR,
25 NPY_USERDEF,
27 NPY_C_CONTIGUOUS,
28 NPY_F_CONTIGUOUS
30 ctypedef class numpy.dtype [object PyArray_Descr]:
31 cdef int type_num
32 cdef object fields
33 cdef object names
36 ctypedef class numpy.ndarray [object PyArrayObject]:
37 cdef __cythonbufferdefaults__ = {"mode": "strided"}
39 cdef:
40 char *data
41 int ndim "nd"
42 npy_intp *shape "dimensions"
43 npy_intp *strides
44 int flags
45 dtype descr
47 # Note: This syntax (function definition in pxd files) is an
48 # experimental exception made for __getbuffer__ and __releasebuffer__
49 # -- the details of this may change.
50 def __getbuffer__(ndarray self, Py_buffer* info, int flags):
51 # This implementation of getbuffer is geared towards Cython
52 # requirements, and does not yet fullfill the PEP.
53 # In particular strided access is always provided regardless
54 # of flags
55 cdef int copy_shape, i, ndim
56 ndim = PyArray_NDIM(self)
58 if sizeof(npy_intp) != sizeof(Py_ssize_t):
59 copy_shape = 1
60 else:
61 copy_shape = 0
63 if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
64 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
65 raise ValueError("ndarray is not C contiguous")
67 if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
68 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
69 raise ValueError("ndarray is not Fortran contiguous")
71 info.buf = PyArray_DATA(self)
72 info.ndim = ndim
73 if copy_shape:
74 # Allocate new buffer for strides and shape info. This is allocated
75 # as one block, strides first.
76 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * ndim * 2)
77 info.shape = info.strides + ndim
78 for i in range(ndim):
79 info.strides[i] = PyArray_STRIDES(self)[i]
80 info.shape[i] = PyArray_DIMS(self)[i]
81 else:
82 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
83 info.shape = <Py_ssize_t*>PyArray_DIMS(self)
84 info.suboffsets = NULL
85 info.itemsize = PyArray_ITEMSIZE(self)
86 info.readonly = not PyArray_ISWRITEABLE(self)
88 cdef int t
89 cdef char* f = NULL
90 cdef dtype descr = self.descr
91 cdef list stack
93 cdef bint hasfields = PyDataType_HASFIELDS(descr)
95 # Ugly hack warning:
96 # Cython currently will not support helper functions in
97 # pxd files -- so we must keep our own, manual stack!
98 # In addition, avoid allocation of the stack in the common
99 # case that we are dealing with a single non-nested datatype...
100 # (this would look much prettier if we could use utility
101 # functions).
103 if not hasfields and not copy_shape:
104 # do not call releasebuffer
105 info.obj = None
106 else:
107 # need to call releasebuffer
108 info.obj = self
110 if not hasfields:
111 t = descr.type_num
112 if t == NPY_BYTE: f = "b"
113 elif t == NPY_UBYTE: f = "B"
114 elif t == NPY_SHORT: f = "h"
115 elif t == NPY_USHORT: f = "H"
116 elif t == NPY_INT: f = "i"
117 elif t == NPY_UINT: f = "I"
118 elif t == NPY_LONG: f = "l"
119 elif t == NPY_ULONG: f = "L"
120 elif t == NPY_LONGLONG: f = "q"
121 elif t == NPY_ULONGLONG: f = "Q"
122 elif t == NPY_FLOAT: f = "f"
123 elif t == NPY_DOUBLE: f = "d"
124 elif t == NPY_LONGDOUBLE: f = "g"
125 elif t == NPY_CFLOAT: f = "Zf"
126 elif t == NPY_CDOUBLE: f = "Zd"
127 elif t == NPY_CLONGDOUBLE: f = "Zg"
128 elif t == NPY_OBJECT: f = "O"
129 else:
130 raise ValueError("unknown dtype code in numpy.pxd (%d)" % t)
131 info.format = f
132 return
133 else:
134 info.format = <char*>stdlib.malloc(255) # static size
135 f = info.format
136 stack = [iter(descr.fields.iteritems())]
138 while True:
139 iterator = stack[-1]
140 descr = None
141 while descr is None:
142 try:
143 descr = iterator.next()[1][0]
144 except StopIteration:
145 stack.pop()
146 if len(stack) > 0:
147 f[0] = 125 #"}"
148 f += 1
149 iterator = stack[-1]
150 else:
151 f[0] = 0 # Terminate string!
152 return
154 hasfields = PyDataType_HASFIELDS(descr)
155 if not hasfields:
156 t = descr.type_num
157 if f - info.format > 240: # this should leave room for "T{" and "}" as well
158 raise RuntimeError("Format string allocated too short.")
160 # Until ticket #99 is fixed, use integers to avoid warnings
161 if t == NPY_BYTE: f[0] = 98 #"b"
162 elif t == NPY_UBYTE: f[0] = 66 #"B"
163 elif t == NPY_SHORT: f[0] = 104 #"h"
164 elif t == NPY_USHORT: f[0] = 72 #"H"
165 elif t == NPY_INT: f[0] = 105 #"i"
166 elif t == NPY_UINT: f[0] = 73 #"I"
167 elif t == NPY_LONG: f[0] = 108 #"l"
168 elif t == NPY_ULONG: f[0] = 76 #"L"
169 elif t == NPY_LONGLONG: f[0] = 113 #"q"
170 elif t == NPY_ULONGLONG: f[0] = 81 #"Q"
171 elif t == NPY_FLOAT: f[0] = 102 #"f"
172 elif t == NPY_DOUBLE: f[0] = 100 #"d"
173 elif t == NPY_LONGDOUBLE: f[0] = 103 #"g"
174 elif t == NPY_CFLOAT: f[0] = 90; f[1] = 102; f += 1
175 elif t == NPY_CDOUBLE: f[0] = 90; f[1] = 100; f += 1
176 elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1
177 elif t == NPY_OBJECT: f[0] = 79 #"O"
178 else:
179 raise ValueError("unknown dtype code in numpy.pxd (%d)" % t)
180 f += 1
181 else:
182 f[0] = 84 #"T"
183 f[1] = 123 #"{"
184 f += 2
185 stack.append(iter(descr.fields.iteritems()))
187 def __releasebuffer__(ndarray self, Py_buffer* info):
188 # This can not be called unless format needs to be freed (as
189 # obj is set to NULL in those case)
190 if PyArray_HASFIELDS(self):
191 stdlib.free(info.format)
192 if sizeof(npy_intp) != sizeof(Py_ssize_t):
193 stdlib.free(info.strides)
194 # info.shape was stored after info.strides in the same block
197 cdef void* PyArray_DATA(ndarray arr)
198 cdef int PyArray_TYPE(ndarray arr)
199 cdef int PyArray_NDIM(ndarray arr)
200 cdef int PyArray_ISWRITEABLE(ndarray arr)
201 cdef npy_intp* PyArray_STRIDES(ndarray arr)
202 cdef npy_intp* PyArray_DIMS(ndarray arr)
203 cdef int PyArray_ITEMSIZE(ndarray arr)
204 cdef int PyArray_CHKFLAGS(ndarray arr, int flags)
205 cdef int PyArray_HASFIELDS(ndarray arr)
207 cdef int PyDataType_HASFIELDS(dtype obj)
209 ctypedef signed int npy_byte
210 ctypedef signed int npy_short
211 ctypedef signed int npy_int
212 ctypedef signed int npy_long
213 ctypedef signed int npy_longlong
215 ctypedef unsigned int npy_ubyte
216 ctypedef unsigned int npy_ushort
217 ctypedef unsigned int npy_uint
218 ctypedef unsigned int npy_ulong
219 ctypedef unsigned int npy_ulonglong
221 ctypedef float npy_float
222 ctypedef float npy_double
223 ctypedef float npy_longdouble
225 ctypedef signed int npy_int8
226 ctypedef signed int npy_int16
227 ctypedef signed int npy_int32
228 ctypedef signed int npy_int64
229 ctypedef signed int npy_int96
230 ctypedef signed int npy_int128
232 ctypedef unsigned int npy_uint8
233 ctypedef unsigned int npy_uint16
234 ctypedef unsigned int npy_uint32
235 ctypedef unsigned int npy_uint64
236 ctypedef unsigned int npy_uint96
237 ctypedef unsigned int npy_uint128
239 ctypedef float npy_float32
240 ctypedef float npy_float64
241 ctypedef float npy_float80
242 ctypedef float npy_float96
243 ctypedef float npy_float128
245 ctypedef struct npy_cfloat:
246 float real
247 float imag
249 ctypedef struct npy_cdouble:
250 double real
251 double imag
253 ctypedef struct npy_clongdouble:
254 long double real
255 long double imag
257 # Typedefs that matches the runtime dtype objects in
258 # the numpy module.
260 # The ones that are commented out needs an IFDEF function
261 # in Cython to enable them only on the right systems.
263 ctypedef npy_int8 int8_t
264 ctypedef npy_int16 int16_t
265 ctypedef npy_int32 int32_t
266 ctypedef npy_int64 int64_t
267 #ctypedef npy_int96 int96_t
268 #ctypedef npy_int128 int128_t
270 ctypedef npy_uint8 uint8_t
271 ctypedef npy_uint16 uint16_t
272 ctypedef npy_uint32 uint32_t
273 ctypedef npy_uint64 uint64_t
274 #ctypedef npy_uint96 uint96_t
275 #ctypedef npy_uint128 uint128_t
277 ctypedef npy_float32 float32_t
278 ctypedef npy_float64 float64_t
279 #ctypedef npy_float80 float80_t
280 #ctypedef npy_float128 float128_t
282 # The int types are mapped a bit surprising --
283 # numpy.int corresponds to 'l' and numpy.long to 'q'
284 ctypedef npy_long int_t
285 ctypedef npy_longlong long_t
287 ctypedef npy_ulong uint_t
288 ctypedef npy_ulonglong ulong_t
290 ctypedef npy_double float_t
291 ctypedef npy_double double_t
292 ctypedef npy_longdouble longdouble_t
294 ctypedef npy_cfloat cfloat_t
295 ctypedef npy_cdouble cdouble_t
296 ctypedef npy_clongdouble clongdouble_t
