| 976 | } |
| 977 | |
| 978 | static npy_intp |
| 979 | PyArray_PyIntAsIntp_ErrMsg(PyObject *o, const char * msg) |
| 980 | { |
| 981 | #if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP) |
| 982 | long long long_value = -1; |
| 983 | #else |
| 984 | long long_value = -1; |
| 985 | #endif |
| 986 | PyObject *obj, *err; |
| 987 | |
| 988 | /* |
| 989 | * Be a bit stricter and not allow bools. |
| 990 | * np.bool_ is also disallowed as Boolean arrays do not currently |
| 991 | * support index. |
| 992 | */ |
| 993 | if (!o || PyBool_Check(o) || PyArray_IsScalar(o, Bool)) { |
| 994 | PyErr_SetString(PyExc_TypeError, msg); |
| 995 | return -1; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Since it is the usual case, first check if o is an integer. This is |
| 1000 | * an exact check, since otherwise __index__ is used. |
| 1001 | */ |
| 1002 | if (PyLong_CheckExact(o)) { |
| 1003 | #if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP) |
| 1004 | long_value = PyLong_AsLongLong(o); |
| 1005 | #else |
| 1006 | long_value = PyLong_AsLong(o); |
| 1007 | #endif |
| 1008 | return (npy_intp)long_value; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * The most general case. PyNumber_Index(o) covers everything |
| 1013 | * including arrays. In principle it may be possible to replace |
| 1014 | * the whole function by PyIndex_AsSSize_t after deprecation. |
| 1015 | */ |
| 1016 | obj = PyNumber_Index(o); |
| 1017 | if (obj == NULL) { |
| 1018 | return -1; |
| 1019 | } |
| 1020 | #if (NPY_SIZEOF_LONG < NPY_SIZEOF_INTP) |
| 1021 | long_value = PyLong_AsLongLong(obj); |
| 1022 | #else |
| 1023 | long_value = PyLong_AsLong(obj); |
| 1024 | #endif |
| 1025 | Py_DECREF(obj); |
| 1026 | |
| 1027 | if (error_converting(long_value)) { |
| 1028 | err = PyErr_Occurred(); |
| 1029 | /* Only replace TypeError's here, which are the normal errors. */ |
| 1030 | if (PyErr_GivenExceptionMatches(err, PyExc_TypeError)) { |
| 1031 | PyErr_SetString(PyExc_TypeError, msg); |
| 1032 | } |
| 1033 | return -1; |
| 1034 | } |
| 1035 | goto overflow_check; /* silence unused warning */ |
no outgoing calls
no test coverage detected