Cython has moved to github.
cython-devel
view Cython/Includes/numpy.pxd @ 2015:fa5aefbc3b0f
Format strings from numpy.pxd: Added endian/packing information
| author | Dag Sverre Seljebotn <dagss@student.matnat.uio.no> |
|---|---|
| date | Sun Apr 19 23:03:09 2009 +0200 (3 years ago) |
| parents | 18b01615d799 |
| children | 453ec5a1ec60 |
line source
1 # NumPy static imports for Cython
2 #
3 # This also defines backwards-compatability buffer acquisition
4 # code for use in Python 2.x (or Python <= 2.5 when NumPy starts
5 # implementing PEP-3118 directly).
8 # Because of laziness, the format string of the buffer is statically
9 # allocated. Increase the size if this is not enough, or submit a
10 # patch to do this properly.
12 DEF _buffer_format_string_len = 255
14 cimport python_buffer as pybuf
15 cimport stdlib
17 cdef extern from "Python.h":
18 ctypedef int Py_intptr_t
20 cdef extern from "numpy/arrayobject.h":
21 ctypedef Py_intptr_t npy_intp
23 cdef enum:
24 NPY_BOOL,
25 NPY_BYTE, NPY_UBYTE,
26 NPY_SHORT, NPY_USHORT,
27 NPY_INT, NPY_UINT,
28 NPY_LONG, NPY_ULONG,
29 NPY_LONGLONG, NPY_ULONGLONG,
30 NPY_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE,
31 NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE,
32 NPY_OBJECT,
33 NPY_STRING, NPY_UNICODE,
34 NPY_VOID,
35 NPY_NTYPES,
36 NPY_NOTYPE,
37 NPY_CHAR,
38 NPY_USERDEF,
40 NPY_C_CONTIGUOUS,
41 NPY_F_CONTIGUOUS
43 ctypedef class numpy.dtype [object PyArray_Descr]:
44 cdef int type_num
45 cdef int itemsize "elsize"
46 cdef char byteorder
47 cdef object fields
48 cdef object names
51 ctypedef class numpy.ndarray [object PyArrayObject]:
52 cdef __cythonbufferdefaults__ = {"mode": "strided"}
54 cdef:
55 char *data
56 int ndim "nd"
57 npy_intp *shape "dimensions"
58 npy_intp *strides
59 int flags
60 dtype descr
62 # Note: This syntax (function definition in pxd files) is an
63 # experimental exception made for __getbuffer__ and __releasebuffer__
64 # -- the details of this may change.
65 def __getbuffer__(ndarray self, Py_buffer* info, int flags):
66 # This implementation of getbuffer is geared towards Cython
67 # requirements, and does not yet fullfill the PEP.
68 # In particular strided access is always provided regardless
69 # of flags
70 cdef int copy_shape, i, ndim
71 ndim = PyArray_NDIM(self)
73 if sizeof(npy_intp) != sizeof(Py_ssize_t):
74 copy_shape = 1
75 else:
76 copy_shape = 0
78 if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
79 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
80 raise ValueError("ndarray is not C contiguous")
82 if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
83 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
84 raise ValueError("ndarray is not Fortran contiguous")
86 info.buf = PyArray_DATA(self)
87 info.ndim = ndim
88 if copy_shape:
89 # Allocate new buffer for strides and shape info. This is allocated
90 # as one block, strides first.
91 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * ndim * 2)
92 info.shape = info.strides + ndim
93 for i in range(ndim):
94 info.strides[i] = PyArray_STRIDES(self)[i]
95 info.shape[i] = PyArray_DIMS(self)[i]
96 else:
97 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
98 info.shape = <Py_ssize_t*>PyArray_DIMS(self)
99 info.suboffsets = NULL
100 info.itemsize = PyArray_ITEMSIZE(self)
101 info.readonly = not PyArray_ISWRITEABLE(self)
103 cdef int t
104 cdef char* f = NULL
105 cdef dtype descr = self.descr
106 cdef list stack
107 cdef int offset
108 cdef char byteorder = 0
110 cdef bint hasfields = PyDataType_HASFIELDS(descr)
112 if not hasfields and not copy_shape:
113 # do not call releasebuffer
114 info.obj = None
115 else:
116 # need to call releasebuffer
117 info.obj = self
119 if not hasfields:
120 t = descr.type_num
121 if t == NPY_BYTE: f = "b"
122 elif t == NPY_UBYTE: f = "B"
123 elif t == NPY_SHORT: f = "h"
124 elif t == NPY_USHORT: f = "H"
125 elif t == NPY_INT: f = "i"
126 elif t == NPY_UINT: f = "I"
127 elif t == NPY_LONG: f = "l"
128 elif t == NPY_ULONG: f = "L"
129 elif t == NPY_LONGLONG: f = "q"
130 elif t == NPY_ULONGLONG: f = "Q"
131 elif t == NPY_FLOAT: f = "f"
132 elif t == NPY_DOUBLE: f = "d"
133 elif t == NPY_LONGDOUBLE: f = "g"
134 elif t == NPY_CFLOAT: f = "Zf"
135 elif t == NPY_CDOUBLE: f = "Zd"
136 elif t == NPY_CLONGDOUBLE: f = "Zg"
137 elif t == NPY_OBJECT: f = "O"
138 else:
139 raise ValueError("unknown dtype code in numpy.pxd (%d)" % t)
140 info.format = f
141 return
142 else:
143 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
144 offset = 0
145 f = _util_dtypestring(descr, info.format,
146 info.format + _buffer_format_string_len,
147 &offset, &byteorder)
148 f[0] = 0 # Terminate format string
150 def __releasebuffer__(ndarray self, Py_buffer* info):
151 if PyArray_HASFIELDS(self):
152 stdlib.free(info.format)
153 if sizeof(npy_intp) != sizeof(Py_ssize_t):
154 stdlib.free(info.strides)
155 # info.shape was stored after info.strides in the same block
158 cdef void* PyArray_DATA(ndarray arr)
159 cdef int PyArray_TYPE(ndarray arr)
160 cdef int PyArray_NDIM(ndarray arr)
161 cdef int PyArray_ISWRITEABLE(ndarray arr)
162 cdef npy_intp* PyArray_STRIDES(ndarray arr)
163 cdef npy_intp* PyArray_DIMS(ndarray arr)
164 cdef int PyArray_ITEMSIZE(ndarray arr)
165 cdef int PyArray_CHKFLAGS(ndarray arr, int flags)
166 cdef int PyArray_HASFIELDS(ndarray arr)
168 cdef int PyDataType_HASFIELDS(dtype obj)
170 ctypedef signed int npy_byte
171 ctypedef signed int npy_short
172 ctypedef signed int npy_int
173 ctypedef signed int npy_long
174 ctypedef signed int npy_longlong
176 ctypedef unsigned int npy_ubyte
177 ctypedef unsigned int npy_ushort
178 ctypedef unsigned int npy_uint
179 ctypedef unsigned int npy_ulong
180 ctypedef unsigned int npy_ulonglong
182 ctypedef float npy_float
183 ctypedef float npy_double
184 ctypedef float npy_longdouble
186 ctypedef signed int npy_int8
187 ctypedef signed int npy_int16
188 ctypedef signed int npy_int32
189 ctypedef signed int npy_int64
190 ctypedef signed int npy_int96
191 ctypedef signed int npy_int128
193 ctypedef unsigned int npy_uint8
194 ctypedef unsigned int npy_uint16
195 ctypedef unsigned int npy_uint32
196 ctypedef unsigned int npy_uint64
197 ctypedef unsigned int npy_uint96
198 ctypedef unsigned int npy_uint128
200 ctypedef float npy_float32
201 ctypedef float npy_float64
202 ctypedef float npy_float80
203 ctypedef float npy_float96
204 ctypedef float npy_float128
206 ctypedef struct npy_cfloat:
207 float real
208 float imag
210 ctypedef struct npy_cdouble:
211 double real
212 double imag
214 ctypedef struct npy_clongdouble:
215 long double real
216 long double imag
218 # Typedefs that matches the runtime dtype objects in
219 # the numpy module.
221 # The ones that are commented out needs an IFDEF function
222 # in Cython to enable them only on the right systems.
224 ctypedef npy_int8 int8_t
225 ctypedef npy_int16 int16_t
226 ctypedef npy_int32 int32_t
227 ctypedef npy_int64 int64_t
228 #ctypedef npy_int96 int96_t
229 #ctypedef npy_int128 int128_t
231 ctypedef npy_uint8 uint8_t
232 ctypedef npy_uint16 uint16_t
233 ctypedef npy_uint32 uint32_t
234 ctypedef npy_uint64 uint64_t
235 #ctypedef npy_uint96 uint96_t
236 #ctypedef npy_uint128 uint128_t
238 ctypedef npy_float32 float32_t
239 ctypedef npy_float64 float64_t
240 #ctypedef npy_float80 float80_t
241 #ctypedef npy_float128 float128_t
243 # The int types are mapped a bit surprising --
244 # numpy.int corresponds to 'l' and numpy.long to 'q'
245 ctypedef npy_long int_t
246 ctypedef npy_longlong long_t
248 ctypedef npy_ulong uint_t
249 ctypedef npy_ulonglong ulong_t
251 ctypedef npy_double float_t
252 ctypedef npy_double double_t
253 ctypedef npy_longdouble longdouble_t
255 ctypedef npy_cfloat cfloat_t
256 ctypedef npy_cdouble cdouble_t
257 ctypedef npy_clongdouble clongdouble_t
260 cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset, char* byteorder) except NULL:
261 # Recursive utility function used in __getbuffer__ to get format
262 # string. The new location in the format string is returned.
264 cdef dtype child
265 cdef int delta_offset
266 cdef tuple i
267 cdef char new_byteorder
268 for i in descr.fields.itervalues():
269 child = i[0]
270 new_offset = i[1]
272 if (end - f) - (new_offset - offset[0]) < 15: # this should leave room for "T{" and "}" as well
273 raise RuntimeError("Format string allocated too short, see comment in numpy.pxd")
275 new_byteorder = child.byteorder
276 if new_byteorder == '|': new_byteorder = '='
277 if byteorder[0] != new_byteorder:
278 f[0] = new_byteorder
279 f += 1
280 byteorder[0] = new_byteorder
282 # Output padding bytes
283 while offset[0] < new_offset:
284 f[0] = 120 # "x"; pad byte
285 f += 1
286 offset[0] += 1
288 offset[0] += child.itemsize
290 if not PyDataType_HASFIELDS(child):
291 t = child.type_num
292 if end - f < 15: # this should leave room for "T{" and "}" as well
293 raise RuntimeError("Format string allocated too short.")
295 # Until ticket #99 is fixed, use integers to avoid warnings
296 if t == NPY_BYTE: f[0] = 98 #"b"
297 elif t == NPY_UBYTE: f[0] = 66 #"B"
298 elif t == NPY_SHORT: f[0] = 104 #"h"
299 elif t == NPY_USHORT: f[0] = 72 #"H"
300 elif t == NPY_INT: f[0] = 105 #"i"
301 elif t == NPY_UINT: f[0] = 73 #"I"
302 elif t == NPY_LONG: f[0] = 108 #"l"
303 elif t == NPY_ULONG: f[0] = 76 #"L"
304 elif t == NPY_LONGLONG: f[0] = 113 #"q"
305 elif t == NPY_ULONGLONG: f[0] = 81 #"Q"
306 elif t == NPY_FLOAT: f[0] = 102 #"f"
307 elif t == NPY_DOUBLE: f[0] = 100 #"d"
308 elif t == NPY_LONGDOUBLE: f[0] = 103 #"g"
309 elif t == NPY_CFLOAT: f[0] = 90; f[1] = 102; f += 1 # Zf
310 elif t == NPY_CDOUBLE: f[0] = 90; f[1] = 100; f += 1 # Zd
311 elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg
312 elif t == NPY_OBJECT: f[0] = 79 #"O"
313 else:
314 raise ValueError("unknown dtype code in numpy.pxd (%d)" % t)
315 f += 1
316 else:
317 f[0] = 84 #"T"
318 f[1] = 123 #"{"
319 f += 2
320 f = _util_dtypestring(child, f, end, offset, byteorder)
321 f[0] = 125 #"}"
322 f += 1
323 return f
