diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2020-09-19 18:37:23 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2020-09-21 06:47:03 -0600 |
commit | f983885f4cafddfe5b5c1e7e19e8d3a04d735fee (patch) | |
tree | 94f783ebac84b2e4d6fffbc667ee8cd6953389ac | |
parent | 3b294521a7394c2195e6b2980555c6b27258bac9 (diff) | |
download | numpy-f983885f4cafddfe5b5c1e7e19e8d3a04d735fee.tar.gz |
ENH: Add PyLong_AsInt function.
This compliments the PyLong_As* functions provided by Python. It
is copied from the Python private function _PyLong_AsInt. It allows
replacing some incorrect uses of PyInt_Check.
-rw-r--r-- | numpy/core/include/numpy/npy_3kcompat.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/numpy/core/include/numpy/npy_3kcompat.h b/numpy/core/include/numpy/npy_3kcompat.h index 4bc06fc96..191cd244f 100644 --- a/numpy/core/include/numpy/npy_3kcompat.h +++ b/numpy/core/include/numpy/npy_3kcompat.h @@ -28,6 +28,30 @@ extern "C" { * PyInt -> PyLong */ + +/* + * This is a renamed copy of the Python non-limited API function _PyLong_AsInt. It is + * included here because it is missing from the PyPy API. It completes the PyLong_As* + * group of functions and can be useful in replacing PyInt_Check. + */ +static NPY_INLINE int +Npy__PyLong_AsInt(PyObject *obj) +{ + int overflow; + long result = PyLong_AsLongAndOverflow(obj, &overflow); + + /* INT_MAX and INT_MIN are defined in Python.h */ + if (overflow || result > INT_MAX || result < INT_MIN) { + /* XXX: could be cute and give a different + message for overflow == -1 */ + PyErr_SetString(PyExc_OverflowError, + "Python int too large to convert to C int"); + return -1; + } + return (int)result; +} + + #if defined(NPY_PY3K) /* Return True only if the long fits in a C long */ static NPY_INLINE int PyInt_Check(PyObject *op) { @@ -39,6 +63,7 @@ static NPY_INLINE int PyInt_Check(PyObject *op) { return (overflow == 0); } + #define PyInt_FromLong PyLong_FromLong #define PyInt_AsLong PyLong_AsLong #define PyInt_AS_LONG PyLong_AsLong |