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

Function PyArray_PythonPyIntFromInt

numpy/core/src/common/npy_argparse.c:28–51  ·  view source on GitHub ↗

* 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

Source from the content-addressed store, hash-verified

26 * @returns 0 on failure and 1 on success (`NPY_FAIL`, `NPY_SUCCEED`)
27 */
28NPY_NO_EXPORT int
29PyArray_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
54typedef int convert(PyObject *, void *);

Callers 1

PyUFunc_GenericReductionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected