|
3 | 3 | http://scipy-lectures.github.io/advanced/interfacing_with_c/interfacing_with_c.html |
4 | 4 | */ |
5 | 5 |
|
| 6 | +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
| 7 | + |
6 | 8 | #include <Python.h> |
7 | 9 | #include <numpy/arrayobject.h> |
8 | 10 |
|
@@ -30,23 +32,23 @@ static PyObject* CAPI_update(PyObject* self, PyObject* args) |
30 | 32 | if (NULL == iarray) return NULL; |
31 | 33 |
|
32 | 34 | /* check to make sure we are a double type */ |
33 | | - if (iarray->descr->type_num != NPY_DOUBLE || |
34 | | - iarray->nd != 2) { |
| 35 | + if (PyArray_DTYPE(iarray)->type_num != NPY_DOUBLE || |
| 36 | + PyArray_NDIM(iarray) != 2) { |
35 | 37 | PyErr_SetString(PyExc_ValueError, "wrong input array type"); |
36 | 38 | return NULL; |
37 | 39 | } |
38 | 40 |
|
39 | 41 | /* get the dimensions */ |
40 | | - n = iarray->dimensions[0]; |
41 | | - m = iarray->dimensions[1]; |
| 42 | + n = PyArray_DIM(iarray, 0); |
| 43 | + m = PyArray_DIM(iarray, 1); |
42 | 44 |
|
43 | 45 | /* change contigous arrays into C ** arrays -- we need to have a |
44 | 46 | vector of pointers that point to the correct location in the |
45 | 47 | contiguous block of memory that stores the multi-dimensional |
46 | 48 | array data */ |
47 | 49 | iA = (double **) malloc( (size_t) (n*sizeof(double))); |
48 | 50 | for (i = 0; i < n; i++) { |
49 | | - iA[i] = (double *) iarray->data + i*m; |
| 51 | + iA[i] = (double *) PyArray_DATA(iarray) + i*m; |
50 | 52 | } |
51 | 53 |
|
52 | 54 | /* now we can do our manipulation */ |
@@ -74,9 +76,19 @@ static PyMethodDef laplace_CAPIMethods[] = { |
74 | 76 | {NULL, NULL, 0, NULL} |
75 | 77 | }; |
76 | 78 |
|
| 79 | +static struct PyModuleDef moduledef = { |
| 80 | + PyModuleDef_HEAD_INIT, |
| 81 | + "laplace_CAPI", // name |
| 82 | + "a simple example: square the elements of an array", // documentation |
| 83 | + -1, // size |
| 84 | + laplace_CAPIMethods, // methods |
| 85 | +}; |
| 86 | + |
77 | 87 | /* this tells python what to do when it first imports this module -- |
78 | 88 | the name follows directly from the table name above */ |
79 | | -PyMODINIT_FUNC initlaplace_CAPI(void) { |
80 | | - (void) Py_InitModule("laplace_CAPI", laplace_CAPIMethods); |
81 | | - import_array(); // this deals with the NumPy stuff |
| 89 | +PyMODINIT_FUNC PyInit_laplace_CAPI(void) { |
| 90 | + PyObject *m; |
| 91 | + m = PyModule_Create(&moduledef); |
| 92 | + import_array(); |
| 93 | + return m; |
82 | 94 | } |
0 commit comments