Skip to content

Commit acac516

Browse files
Updated lazylinker C code
Some macros were removed from npy_3k_compat.h. Following numpy, I updated the affected functions to the Python 3 names, and removed support for Python 2.
1 parent 19226b9 commit acac516

File tree

2 files changed

+19
-40
lines changed

2 files changed

+19
-40
lines changed

pytensor/link/c/c_code/lazylinker_c.c

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
#if PY_VERSION_HEX >= 0x03000000
77
#include "numpy/npy_3kcompat.h"
8-
#define PyCObject_AsVoidPtr NpyCapsule_AsVoidPtr
9-
#define PyCObject_GetDesc NpyCapsule_GetDesc
10-
#define PyCObject_Check NpyCapsule_Check
118
#endif
129

1310
#ifndef Py_TYPE
@@ -323,9 +320,9 @@ static int CLazyLinker_init(CLazyLinker *self, PyObject *args, PyObject *kwds) {
323320
if (PyObject_HasAttrString(thunk, "cthunk")) {
324321
PyObject *cthunk = PyObject_GetAttrString(thunk, "cthunk");
325322
// new reference
326-
assert(cthunk && PyCObject_Check(cthunk));
327-
self->thunk_cptr_fn[i] = PyCObject_AsVoidPtr(cthunk);
328-
self->thunk_cptr_data[i] = PyCObject_GetDesc(cthunk);
323+
assert(cthunk && NpyCapsule_Check(cthunk));
324+
self->thunk_cptr_fn[i] = NpyCapsule_AsVoidPtr(cthunk);
325+
self->thunk_cptr_data[i] = NpyCapsule_GetDesc(cthunk);
329326
Py_DECREF(cthunk);
330327
// cthunk is kept alive by membership in self->thunks
331328
}
@@ -487,8 +484,8 @@ static PyObject *pycall(CLazyLinker *self, Py_ssize_t node_idx, int verbose) {
487484
PyList_SetItem(self->call_times, node_idx,
488485
PyFloat_FromDouble(t1 - t0 + ti));
489486
PyObject *count = PyList_GetItem(self->call_counts, node_idx);
490-
long icount = PyInt_AsLong(count);
491-
PyList_SetItem(self->call_counts, node_idx, PyInt_FromLong(icount + 1));
487+
long icount = PyLong_AsLong(count);
488+
PyList_SetItem(self->call_counts, node_idx, PyLong_FromLong(icount + 1));
492489
}
493490
} else {
494491
if (verbose) {
@@ -512,8 +509,8 @@ static int c_call(CLazyLinker *self, Py_ssize_t node_idx, int verbose) {
512509
PyList_SetItem(self->call_times, node_idx,
513510
PyFloat_FromDouble(t1 - t0 + ti));
514511
PyObject *count = PyList_GetItem(self->call_counts, node_idx);
515-
long icount = PyInt_AsLong(count);
516-
PyList_SetItem(self->call_counts, node_idx, PyInt_FromLong(icount + 1));
512+
long icount = PyLong_AsLong(count);
513+
PyList_SetItem(self->call_counts, node_idx, PyLong_FromLong(icount + 1));
517514
} else {
518515
err = fn(self->thunk_cptr_data[node_idx]);
519516
}
@@ -774,20 +771,20 @@ static PyObject *CLazyLinker_call(PyObject *_self, PyObject *args,
774771
output_subset = (char *)calloc(self->n_output_vars, sizeof(char));
775772
for (int it = 0; it < output_subset_size; ++it) {
776773
PyObject *elem = PyList_GetItem(output_subset_ptr, it);
777-
if (!PyInt_Check(elem)) {
774+
if (!PyLong_Check(elem)) {
778775
err = 1;
779776
PyErr_SetString(PyExc_RuntimeError,
780777
"Some elements of output_subset list are not int");
781778
}
782-
output_subset[PyInt_AsLong(elem)] = 1;
779+
output_subset[PyLong_AsLong(elem)] = 1;
783780
}
784781
}
785782
}
786783

787784
self->position_of_error = -1;
788785
// create constants used to fill the var_compute_cells
789-
PyObject *one = PyInt_FromLong(1);
790-
PyObject *zero = PyInt_FromLong(0);
786+
PyObject *one = PyLong_FromLong(1);
787+
PyObject *zero = PyLong_FromLong(0);
791788

792789
// pre-allocate our return value
793790
Py_INCREF(Py_None);
@@ -942,11 +939,8 @@ static PyMemberDef CLazyLinker_members[] = {
942939
};
943940

944941
static PyTypeObject lazylinker_ext_CLazyLinkerType = {
945-
#if defined(NPY_PY3K)
946942
PyVarObject_HEAD_INIT(NULL, 0)
947-
#else
948-
PyObject_HEAD_INIT(NULL) 0, /*ob_size*/
949-
#endif
943+
950944
"lazylinker_ext.CLazyLinker", /*tp_name*/
951945
sizeof(CLazyLinker), /*tp_basicsize*/
952946
0, /*tp_itemsize*/
@@ -996,7 +990,7 @@ static PyMethodDef lazylinker_ext_methods[] = {
996990
{NULL, NULL, 0, NULL} /* Sentinel */
997991
};
998992

999-
#if defined(NPY_PY3K)
993+
1000994
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
1001995
"lazylinker_ext",
1002996
NULL,
@@ -1006,28 +1000,19 @@ static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
10061000
NULL,
10071001
NULL,
10081002
NULL};
1009-
#endif
1010-
#if defined(NPY_PY3K)
1011-
#define RETVAL m
1003+
10121004
PyMODINIT_FUNC PyInit_lazylinker_ext(void) {
1013-
#else
1014-
#define RETVAL
1015-
PyMODINIT_FUNC initlazylinker_ext(void) {
1016-
#endif
1005+
10171006
PyObject *m;
10181007

10191008
lazylinker_ext_CLazyLinkerType.tp_new = PyType_GenericNew;
10201009
if (PyType_Ready(&lazylinker_ext_CLazyLinkerType) < 0)
1021-
return RETVAL;
1022-
#if defined(NPY_PY3K)
1010+
return NULL;
1011+
10231012
m = PyModule_Create(&moduledef);
1024-
#else
1025-
m = Py_InitModule3("lazylinker_ext", lazylinker_ext_methods,
1026-
"Example module that creates an extension type.");
1027-
#endif
10281013
Py_INCREF(&lazylinker_ext_CLazyLinkerType);
10291014
PyModule_AddObject(m, "CLazyLinker",
10301015
(PyObject *)&lazylinker_ext_CLazyLinkerType);
10311016

1032-
return RETVAL;
1017+
return m;
10331018
}

pytensor/link/c/c_code/pytensor_mod_helper.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@
1818
#define PYTENSOR_EXTERN
1919
#endif
2020

21-
#if PY_MAJOR_VERSION < 3
22-
#define PYTENSOR_RTYPE void
23-
#else
24-
#define PYTENSOR_RTYPE PyObject *
25-
#endif
26-
2721
/* We need to redefine PyMODINIT_FUNC to add MOD_PUBLIC in the middle */
2822
#undef PyMODINIT_FUNC
29-
#define PyMODINIT_FUNC PYTENSOR_EXTERN MOD_PUBLIC PYTENSOR_RTYPE
23+
#define PyMODINIT_FUNC PYTENSOR_EXTERN MOD_PUBLIC PyObject *
3024

3125
#endif

0 commit comments

Comments
 (0)