* Small wrapper converting to array just like CPython does. * * We could use our own PyArray_PyIntAsInt function, but it handles floats * differently. * A disadvantage of this function compared to ``PyArg_*("i")`` code is that * it will not say which parameter is wrong. * * @param obj The python object to convert * @param value The output value * * @returns 0 on failure and 1 on success
| 26 | * @returns 0 on failure and 1 on success (`NPY_FAIL`, `NPY_SUCCEED`) |
| 27 | */ |
| 28 | NPY_NO_EXPORT int |
| 29 | PyArray_PythonPyIntFromInt(PyObject *obj, int *value) |
| 30 | { |
| 31 | /* Pythons behaviour is to check only for float explicitly... */ |
| 32 | if (NPY_UNLIKELY(PyFloat_Check(obj))) { |
| 33 | PyErr_SetString(PyExc_TypeError, |
| 34 | "integer argument expected, got float"); |
| 35 | return NPY_FAIL; |
| 36 | } |
| 37 | |
| 38 | long result = PyLong_AsLong(obj); |
| 39 | if (NPY_UNLIKELY((result == -1) && PyErr_Occurred())) { |
| 40 | return NPY_FAIL; |
| 41 | } |
| 42 | if (NPY_UNLIKELY((result > INT_MAX) || (result < INT_MIN))) { |
| 43 | PyErr_SetString(PyExc_OverflowError, |
| 44 | "Python int too large to convert to C int"); |
| 45 | return NPY_FAIL; |
| 46 | } |
| 47 | else { |
| 48 | *value = (int)result; |
| 49 | return NPY_SUCCEED; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | typedef int convert(PyObject *, void *); |
no outgoing calls
no test coverage detected