MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_PyIntAsIntp_ErrMsg

Function PyArray_PyIntAsIntp_ErrMsg

numpy/core/src/multiarray/conversion_utils.c:978–1056  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

976}
977
978static npy_intp
979PyArray_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 */

Callers 2

PyArray_PyIntAsIntpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected