diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-12-01 16:27:48 +0000 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-12-02 09:28:44 +0000 |
commit | 3c6bbd7b43bf584aeef780a9e42acf2ecce7b179 (patch) | |
tree | ef8cae52c02e714a58861bbfbb4783cb0d5696ca | |
parent | 6fa0ff8e56e7e09f0c1cdbce2da338be073947e9 (diff) | |
download | numpy-3c6bbd7b43bf584aeef780a9e42acf2ecce7b179.tar.gz |
ENH: Chain exceptions when converting python objects to fortran
Note that since we now need `npy_3kcompat.h`, we can delete all duplicated lines in this file
We preserve the original exception type for compatibility with old code here.
-rw-r--r-- | numpy/core/include/numpy/npy_3kcompat.h | 1 | ||||
-rwxr-xr-x | numpy/f2py/rules.py | 12 | ||||
-rw-r--r-- | numpy/f2py/src/fortranobject.h | 25 |
3 files changed, 10 insertions, 28 deletions
diff --git a/numpy/core/include/numpy/npy_3kcompat.h b/numpy/core/include/numpy/npy_3kcompat.h index 832bc0599..7ed263796 100644 --- a/numpy/core/include/numpy/npy_3kcompat.h +++ b/numpy/core/include/numpy/npy_3kcompat.h @@ -45,6 +45,7 @@ static NPY_INLINE int PyInt_Check(PyObject *op) { #define PyInt_AsLong PyLong_AsLong #define PyInt_AS_LONG PyLong_AsLong #define PyInt_AsSsize_t PyLong_AsSsize_t +#define PyNumber_Int PyNumber_Long /* NOTE: * diff --git a/numpy/f2py/rules.py b/numpy/f2py/rules.py index f2f713bde..28eb9da30 100755 --- a/numpy/f2py/rules.py +++ b/numpy/f2py/rules.py @@ -1064,8 +1064,10 @@ if (#varname#_capi==Py_None) { '\tcapi_#varname#_tmp = array_from_pyobj(#atype#,#varname#_Dims,#varname#_Rank,capi_#varname#_intent,#varname#_capi);'}, """\ \tif (capi_#varname#_tmp == NULL) { -\t\tif (!PyErr_Occurred()) -\t\t\tPyErr_SetString(#modulename#_error,\"failed in converting #nth# `#varname#\' of #pyname# to C/Fortran array\" ); +\t\tPyObject *exc, *val, *tb; +\t\tPyErr_Fetch(&exc, &val, &tb); +\t\tPyErr_SetString(exc ? exc : #modulename#_error,\"failed in converting #nth# `#varname#\' of #pyname# to C/Fortran array\" ); +\t\tnpy_PyErr_ChainExceptionsCause(exc, val, tb); \t} else { \t\t#varname# = (#ctype# *)(PyArray_DATA(capi_#varname#_tmp)); """, @@ -1081,8 +1083,10 @@ if (#varname#_capi==Py_None) { \t\t\twhile ((_i = nextforcomb())) \t\t\t\t#varname#[capi_i++] = #init#; /* fortran way */ \t\t} else { -\t\t\tif (!PyErr_Occurred()) -\t\t\t\tPyErr_SetString(#modulename#_error,\"Initialization of #nth# #varname# failed (initforcomb).\"); +\t\t\tPyObject *exc, *val, *tb; +\t\t\tPyErr_Fetch(&exc, &val, &tb); +\t\t\tPyErr_SetString(exc ? exc : #modulename#_error,\"Initialization of #nth# #varname# failed (initforcomb).\"); +\t\t\tnpy_PyErr_ChainExceptionsCause(exc, val, tb); \t\t\tf2py_success = 0; \t\t} \t} diff --git a/numpy/f2py/src/fortranobject.h b/numpy/f2py/src/fortranobject.h index 5d0dcf676..21f1977eb 100644 --- a/numpy/f2py/src/fortranobject.h +++ b/numpy/f2py/src/fortranobject.h @@ -11,30 +11,7 @@ extern "C" { #endif #define PY_ARRAY_UNIQUE_SYMBOL _npy_f2py_ARRAY_API #include "numpy/arrayobject.h" - -/* - * Python 3 support macros - */ -#if PY_VERSION_HEX >= 0x03000000 -#define PyString_Check PyBytes_Check -#define PyString_GET_SIZE PyBytes_GET_SIZE -#define PyString_AS_STRING PyBytes_AS_STRING -#define PyString_FromString PyBytes_FromString -#define PyUString_FromStringAndSize PyUnicode_FromStringAndSize -#define PyString_ConcatAndDel PyBytes_ConcatAndDel -#define PyString_AsString PyBytes_AsString - -#define PyInt_Check PyLong_Check -#define PyInt_FromLong PyLong_FromLong -#define PyInt_AS_LONG PyLong_AsLong -#define PyInt_AsLong PyLong_AsLong - -#define PyNumber_Int PyNumber_Long - -#else - -#define PyUString_FromStringAndSize PyString_FromStringAndSize -#endif +#include "numpy/npy_3kcompat.h" #ifdef F2PY_REPORT_ATEXIT |