Basic numpy testcase working
[cython.git] / Cython / Includes / numpy.pxd
1 cdef extern from "Python.h":
2     ctypedef int Py_intptr_t
3     
4 cdef extern from "numpy/arrayobject.h":
5     ctypedef Py_intptr_t npy_intp
6     ctypedef struct PyArray_Descr:
7         int elsize
8
9     ctypedef class numpy.ndarray [object PyArrayObject]:
10         cdef:
11             char *data
12             int nd
13             npy_intp *dimensions 
14             npy_intp *strides
15             object base
16             # descr not implemented yet here...
17             int flags
18             int itemsize
19             object weakreflist
20             PyArray_Descr* descr
21
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")
25
26             cdef int typenum = PyArray_TYPE(self)
27             
28             info.buf = <void*>self.data
29             info.ndim = 2
30             info.strides = <Py_ssize_t*>self.strides
31             info.shape = <Py_ssize_t*>self.dimensions
32             info.suboffsets = NULL
33             info.format = "i"
34             info.itemsize = self.descr.elsize
35             info.readonly = not PyArray_ISWRITEABLE(self)
36
37             # PS TODO TODO!: Py_ssize_t vs Py_intptr_t
38
39             
40 ##   PyArrayObject *arr = (PyArrayObject*)obj;
41 ##   PyArray_Descr *type = (PyArray_Descr*)arr->descr;
42
43   
44 ##   int typenum = PyArray_TYPE(obj);
45 ##   if (!PyTypeNum_ISNUMBER(typenum)) {
46 ##     PyErr_Format(PyExc_TypeError, "Only numeric NumPy types currently supported.");
47 ##     return -1;
48 ##   }
49
50 ##   /*
51 ##   NumPy format codes doesn't completely match buffer codes;
52 ##   seems safest to retranslate.
53 ##                             01234567890123456789012345*/
54 ##   const char* base_codes = "?bBhHiIlLqQfdgfdgO";
55
56 ##   char* format = (char*)malloc(4);
57 ##   char* fp = format;
58 ##   *fp++ = type->byteorder;
59 ##   if (PyTypeNum_ISCOMPLEX(typenum)) *fp++ = 'Z';
60 ##   *fp++ = base_codes[typenum];
61 ##   *fp = 0;
62
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;
71
72 ##   view->internal = 0;
73 ##   return 0;
74 ##             print "hello" + str(43) + "asdf" + "three"
75 ##             pass
76
77     cdef int PyArray_TYPE(ndarray arr)
78     cdef int PyArray_ISWRITEABLE(ndarray arr)
79
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
87
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
93
94
95 ctypedef npy_int64 Tint64