Skip to content

Commit 64cffe1

Browse files
committed
MAINT: restore npy_PyErr_ChainExceptions
1 parent 1cc89e7 commit 64cffe1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

numpy/_core/include/numpy/npy_3kcompat.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,32 @@ npy_PyFile_CloseFile(PyObject *file)
245245
return 0;
246246
}
247247

248+
/* This is a copy of _PyErr_ChainExceptions, which
249+
* is no longer exported from Python3.12
250+
*/
251+
static inline void
252+
npy_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
253+
{
254+
if (exc == NULL)
255+
return;
248256

249-
#define npy_PyErr_ChainExceptions _PyErr_ChainExceptions
250-
257+
if (PyErr_Occurred()) {
258+
PyObject *exc2, *val2, *tb2;
259+
PyErr_Fetch(&exc2, &val2, &tb2);
260+
PyErr_NormalizeException(&exc, &val, &tb);
261+
if (tb != NULL) {
262+
PyException_SetTraceback(val, tb);
263+
Py_DECREF(tb);
264+
}
265+
Py_DECREF(exc);
266+
PyErr_NormalizeException(&exc2, &val2, &tb2);
267+
PyException_SetContext(val2, val);
268+
PyErr_Restore(exc2, val2, tb2);
269+
}
270+
else {
271+
PyErr_Restore(exc, val, tb);
272+
}
273+
}
251274

252275
/* This is a copy of _PyErr_ChainExceptions, with:
253276
* __cause__ used instead of __context__

0 commit comments

Comments
 (0)