From: Dag Sverre Seljebotn Date: Sat, 2 Aug 2008 09:20:35 +0000 (+0200) Subject: Basic numpy testcase working X-Git-Tag: 0.9.8.1~49^2~30 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=18ba3a48158be7c0248ba9a43ce8b6ebed42bb5a;p=cython.git Basic numpy testcase working --- diff --git a/Cython/Includes/numpy.pxd b/Cython/Includes/numpy.pxd index 5965b1e6..e09fc05d 100644 --- a/Cython/Includes/numpy.pxd +++ b/Cython/Includes/numpy.pxd @@ -2,23 +2,39 @@ cdef extern from "Python.h": ctypedef int Py_intptr_t cdef extern from "numpy/arrayobject.h": - ctypedef void PyArrayObject - int PyArray_TYPE(PyObject* arr) - + ctypedef Py_intptr_t npy_intp + ctypedef struct PyArray_Descr: + int elsize + ctypedef class numpy.ndarray [object PyArrayObject]: cdef: char *data int nd - Py_intptr_t *dimensions - Py_intptr_t *strides + npy_intp *dimensions + npy_intp *strides object base # descr not implemented yet here... int flags int itemsize object weakreflist + PyArray_Descr* descr + + def __getbuffer__(ndarray self, Py_buffer* info, int flags): + if sizeof(npy_intp) != sizeof(Py_ssize_t): + raise RuntimeError("Py_intptr_t and Py_ssize_t differs in size, numpy.pxd does not support this") - def __getbuffer__(self, Py_buffer* info, int flags): cdef int typenum = PyArray_TYPE(self) + + info.buf = self.data + info.ndim = 2 + info.strides = self.strides + info.shape = self.dimensions + info.suboffsets = NULL + info.format = "i" + info.itemsize = self.descr.elsize + info.readonly = not PyArray_ISWRITEABLE(self) + + # PS TODO TODO!: Py_ssize_t vs Py_intptr_t ## PyArrayObject *arr = (PyArrayObject*)obj; @@ -58,7 +74,8 @@ cdef extern from "numpy/arrayobject.h": ## print "hello" + str(43) + "asdf" + "three" ## pass - + cdef int PyArray_TYPE(ndarray arr) + cdef int PyArray_ISWRITEABLE(ndarray arr) ctypedef unsigned int npy_uint8 ctypedef unsigned int npy_uint16 diff --git a/tests/run/tnumpy.pyx b/tests/run/tnumpy.pyx new file mode 100644 index 00000000..a418cf2a --- /dev/null +++ b/tests/run/tnumpy.pyx @@ -0,0 +1,22 @@ +# cannot be named "numpy" in order to no clash with the numpy module! + +cimport numpy + +try: + import numpy + __doc__ = """ + + >>> basic() + [[0 1 2 3 4] + [5 6 7 8 9]] + 2 0 9 5 + + +""" +except: + __doc__ = "" + +def basic(): + cdef object[int, 2] buf = numpy.arange(10).reshape((2, 5)) + print buf + print buf[0, 2], buf[0, 0], buf[1, 4], buf[1, 0]