Skip to content

Commit e9b8a91

Browse files
committed
update for python 3
1 parent 8d67997 commit e9b8a91

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

examples/extensions/timing/laplace_CAPI.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
http://scipy-lectures.github.io/advanced/interfacing_with_c/interfacing_with_c.html
44
*/
55

6+
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
7+
68
#include <Python.h>
79
#include <numpy/arrayobject.h>
810

@@ -30,23 +32,23 @@ static PyObject* CAPI_update(PyObject* self, PyObject* args)
3032
if (NULL == iarray) return NULL;
3133

3234
/* 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) {
3537
PyErr_SetString(PyExc_ValueError, "wrong input array type");
3638
return NULL;
3739
}
3840

3941
/* 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);
4244

4345
/* change contigous arrays into C ** arrays -- we need to have a
4446
vector of pointers that point to the correct location in the
4547
contiguous block of memory that stores the multi-dimensional
4648
array data */
4749
iA = (double **) malloc( (size_t) (n*sizeof(double)));
4850
for (i = 0; i < n; i++) {
49-
iA[i] = (double *) iarray->data + i*m;
51+
iA[i] = (double *) PyArray_DATA(iarray) + i*m;
5052
}
5153

5254
/* now we can do our manipulation */
@@ -74,9 +76,19 @@ static PyMethodDef laplace_CAPIMethods[] = {
7476
{NULL, NULL, 0, NULL}
7577
};
7678

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+
7787
/* this tells python what to do when it first imports this module --
7888
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;
8294
}

0 commit comments

Comments
 (0)