Skip to content

Commit 5954b85

Browse files
committed
MAINT: restore Npy__PyLong_AsInt
1 parent 64cffe1 commit 5954b85

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

numpy/_core/include/numpy/npy_3kcompat.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,32 @@
2020
extern "C" {
2121
#endif
2222

23-
/*
24-
* Macros to protect CRT calls against instant termination when passed an
25-
* invalid parameter (https://bugs.python.org/issue23524).
26-
*/
23+
/* Python13 removes _PyLong_AsInt */
24+
static inline int
25+
Npy__PyLong_AsInt(PyObject *obj)
26+
{
27+
int overflow;
28+
long result = PyLong_AsLongAndOverflow(obj, &overflow);
29+
30+
/* INT_MAX and INT_MIN are defined in Python.h */
31+
if (overflow || result > INT_MAX || result < INT_MIN) {
32+
/* XXX: could be cute and give a different
33+
message for overflow == -1 */
34+
PyErr_SetString(PyExc_OverflowError,
35+
"Python int too large to convert to C int");
36+
return -1;
37+
}
38+
return (int)result;
39+
}
40+
2741
#if defined _MSC_VER && _MSC_VER >= 1900
2842

2943
#include <stdlib.h>
3044

45+
/*
46+
* Macros to protect CRT calls against instant termination when passed an
47+
* invalid parameter (https://bugs.python.org/issue23524).
48+
*/
3149
extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
3250
#define NPY_BEGIN_SUPPRESS_IPH { _invalid_parameter_handler _Py_old_handler = \
3351
_set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler);
@@ -40,7 +58,6 @@ extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
4058

4159
#endif /* _MSC_VER >= 1900 */
4260

43-
4461
/*
4562
* PyFile_* compatibility
4663
*/

numpy/f2py/cfuncs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,13 @@
878878
PyObject* tmp = NULL;
879879
880880
if (PyLong_Check(obj)) {
881-
*v = _PyLong_AsInt(obj);
881+
*v = Npy__PyLong_AsInt(obj);
882882
return !(*v == -1 && PyErr_Occurred());
883883
}
884884
885885
tmp = PyNumber_Long(obj);
886886
if (tmp) {
887-
*v = _PyLong_AsInt(tmp);
887+
*v = Npy__PyLong_AsInt(tmp);
888888
Py_DECREF(tmp);
889889
return !(*v == -1 && PyErr_Occurred());
890890
}

0 commit comments

Comments
 (0)