Repository navigation
Expand file tree
/
Copy path_numpyadapt.c
More file actions
71 lines (56 loc) · 1.4 KB
/
Copy path_numpyadapt.c
File metadata and controls
71 lines (56 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "_pymodule.h"
#include <stdio.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/ndarrayobject.h>
static
int adapt_ndarray(PyObject *obj, void* arystruct) {
PyArrayObject *ndary;
int ndim;
npy_intp *dims;
npy_intp *strides;
void *data;
void **dataptr;
void **objectptr;
npy_intp *dimsptr;
npy_intp *stridesptr;
int i;
if (!PyArray_Check(obj)) {
return -1;
}
ndary = (PyArrayObject*)obj;
ndim = PyArray_NDIM(ndary);
dims = PyArray_DIMS(ndary);
strides = PyArray_STRIDES(ndary);
data = PyArray_DATA(ndary);
dataptr = (void**)arystruct;
dimsptr = (npy_intp*)(dataptr + 1);
stridesptr = dimsptr + ndim;
objectptr = stridesptr + ndim;
for (i = 0; i < ndim; ++i) {
dimsptr[i] = dims[i];
stridesptr[i] = strides[i];
}
*dataptr = data;
*objectptr = obj;
return 0;
}
static
PyObject* get_ndarray_adaptor(PyObject* self, PyObject *args)
{
return PyLong_FromVoidPtr(&adapt_ndarray);
}
static PyMethodDef ext_methods[] = {
#define declmethod(func) { #func , ( PyCFunction )func , METH_VARARGS , NULL }
declmethod(get_ndarray_adaptor),
{ NULL },
#undef declmethod
};
MOD_INIT(_numpyadapt)
{
PyObject *m;
import_array();
MOD_DEF(m, "_numpyadapt", "No docs", ext_methods)
if (m == NULL)
return MOD_ERROR_VAL;
return MOD_SUCCESS_VAL(m);
}