1 cdef extern from "Python.h":
2 ctypedef int Py_intptr_t
4 cdef extern from "numpy/arrayobject.h":
5 ctypedef Py_intptr_t npy_intp
6 ctypedef struct PyArray_Descr:
9 ctypedef class numpy.ndarray [object PyArrayObject]:
16 # descr not implemented yet here...
22 def __getbuffer__(ndarray self, Py_buffer* info, int flags):
23 if sizeof(npy_intp) != sizeof(Py_ssize_t):
24 raise RuntimeError("Py_intptr_t and Py_ssize_t differs in size, numpy.pxd does not support this")
26 cdef int typenum = PyArray_TYPE(self)
28 info.buf = <void*>self.data
30 info.strides = <Py_ssize_t*>self.strides
31 info.shape = <Py_ssize_t*>self.dimensions
32 info.suboffsets = NULL
34 info.itemsize = self.descr.elsize
35 info.readonly = not PyArray_ISWRITEABLE(self)
37 # PS TODO TODO!: Py_ssize_t vs Py_intptr_t
40 ## PyArrayObject *arr = (PyArrayObject*)obj;
41 ## PyArray_Descr *type = (PyArray_Descr*)arr->descr;
44 ## int typenum = PyArray_TYPE(obj);
45 ## if (!PyTypeNum_ISNUMBER(typenum)) {
46 ## PyErr_Format(PyExc_TypeError, "Only numeric NumPy types currently supported.");
51 ## NumPy format codes doesn't completely match buffer codes;
52 ## seems safest to retranslate.
53 ## 01234567890123456789012345*/
54 ## const char* base_codes = "?bBhHiIlLqQfdgfdgO";
56 ## char* format = (char*)malloc(4);
58 ## *fp++ = type->byteorder;
59 ## if (PyTypeNum_ISCOMPLEX(typenum)) *fp++ = 'Z';
60 ## *fp++ = base_codes[typenum];
63 ## view->buf = arr->data;
64 ## view->readonly = !PyArray_ISWRITEABLE(obj);
65 ## view->ndim = PyArray_NDIM(arr);
66 ## view->strides = PyArray_STRIDES(arr);
67 ## view->shape = PyArray_DIMS(arr);
68 ## view->suboffsets = NULL;
69 ## view->format = format;
70 ## view->itemsize = type->elsize;
72 ## view->internal = 0;
74 ## print "hello" + str(43) + "asdf" + "three"
77 cdef int PyArray_TYPE(ndarray arr)
78 cdef int PyArray_ISWRITEABLE(ndarray arr)
80 ctypedef unsigned int npy_uint8
81 ctypedef unsigned int npy_uint16
82 ctypedef unsigned int npy_uint32
83 ctypedef unsigned int npy_uint64
84 ctypedef unsigned int npy_uint96
85 ctypedef unsigned int npy_uint128
86 ctypedef signed int npy_int64
88 ctypedef float npy_float32
89 ctypedef float npy_float64
90 ctypedef float npy_float80
91 ctypedef float npy_float96
92 ctypedef float npy_float128
95 ctypedef npy_int64 Tint64