Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
long
PyInt_AsLong(register PyObject *op)
{
PyNumberMethods *nb;
PyIntObject *io;
long val;
if (op && PyInt_Check(op))
return PyInt_AS_LONG((PyIntObject*) op);
if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
nb->nb_int == NULL) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
}
io = (PyIntObject*) (*nb->nb_int) (op);
if (io == NULL)
return -1;
if (!PyInt_Check(io)) {
if (PyLong_Check(io)) {
/* got a long? => retry int conversion */
val = PyLong_AsLong((PyObject *)io);
Py_DECREF(io);
if ((val == -1) && PyErr_Occurred())
return -1;
return val;
}
else
{
Py_DECREF(io);
PyErr_SetString(PyExc_TypeError,
"__int__ method should return an integer");
return -1;
}
}
val = PyInt_AS_LONG(io);
Py_DECREF(io);
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment