--- ./Objects/longobject-orig.c 2009-12-20 21:25:15.712765366 -0800 +++ ./Objects/longobject.c 2009-12-20 21:25:27.000262792 -0800 @@ -227,49 +227,110 @@ Returns -1 and sets an error condition if overflow occurs. */ long -PyLong_AsLong(PyObject *vv) +PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) { /* This version by Tim Peters */ register PyLongObject *v; unsigned long x, prev; + long res; Py_ssize_t i; int sign; + int do_decref = 0; /* if nb_int was called */ - if (vv == NULL || !PyLong_Check(vv)) { - if (vv != NULL && PyInt_Check(vv)) - return PyInt_AsLong(vv); + *overflow = 0; + if (vv == NULL) { PyErr_BadInternalCall(); return -1; } - v = (PyLongObject *)vv; - i = v->ob_size; - sign = 1; - x = 0; - if (i < 0) { - sign = -1; - i = -(i); - } - while (--i >= 0) { - prev = x; - x = (x << PyLong_SHIFT) | v->ob_digit[i]; - if ((x >> PyLong_SHIFT) != prev) - goto overflow; + + if(PyInt_Check(vv)) + return PyInt_AsLong(vv); + + if (!PyLong_Check(vv)) { + PyNumberMethods *nb; + if ((nb = vv->ob_type->tp_as_number) == NULL || + nb->nb_int == NULL) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + vv = (*nb->nb_int) (vv); + if (vv == NULL) + return -1; + do_decref = 1; + if(PyInt_Check(vv)) { + res = PyInt_AsLong(vv); + goto exit; + } + if (!PyLong_Check(vv)) { + Py_DECREF(vv); + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); + return -1; + } } - /* Haven't lost any bits, but casting to long requires extra care - * (see comment above). - */ - if (x <= (unsigned long)LONG_MAX) { - return (long)x * sign; + + res = -1; + v = (PyLongObject *)vv; + i = Py_SIZE(v); + + switch (i) { + case -1: + res = -(sdigit)v->ob_digit[0]; + break; + case 0: + res = 0; + break; + case 1: + res = v->ob_digit[0]; + break; + default: + sign = 1; + x = 0; + if (i < 0) { + sign = -1; + i = -(i); + } + while (--i >= 0) { + prev = x; + x = (x << PyLong_SHIFT) + v->ob_digit[i]; + if ((x >> PyLong_SHIFT) != prev) { + *overflow = Py_SIZE(v) > 0 ? 1 : -1; + goto exit; + } + } + /* Haven't lost any bits, but casting to long requires extra care + * (see comment above). + */ + if (x <= (unsigned long)LONG_MAX) { + res = (long)x * sign; + } + else if (sign < 0 && x == PY_ABS_LONG_MIN) { + res = LONG_MIN; + } + else { + *overflow = Py_SIZE(v) > 0 ? 1 : -1; + /* res is already set to -1 */ + } } - else if (sign < 0 && x == PY_ABS_LONG_MIN) { - return LONG_MIN; + exit: + if (do_decref) { + Py_DECREF(vv); } - /* else overflow */ + return res; +} - overflow: - PyErr_SetString(PyExc_OverflowError, - "long int too large to convert to int"); - return -1; +long +PyLong_AsLong(PyObject *obj) +{ + int overflow; + long result = PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow) { + /* XXX: could be cute and give a different + message for overflow == -1 */ + PyErr_SetString(PyExc_OverflowError, + "Python int too large to convert to C long"); + } + return result; } /* Get a Py_ssize_t from a long int object. --- ./Include/longobject-orig.h 2009-12-20 21:26:40.402497285 -0800 +++ ./Include/longobject.h 2009-12-20 21:27:23.180258130 -0800 @@ -21,6 +21,7 @@ PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t); PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t); PyAPI_FUNC(long) PyLong_AsLong(PyObject *); +PyAPI_FUNC(long) PyLong_AsLongAndOverflow(PyObject *, int *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *); PyAPI_FUNC(Py_ssize_t) PyLong_AsSsize_t(PyObject *); --- ./Modules/_testcapimodule-orig.c 2009-12-20 21:28:15.540255072 -0800 +++ ./Modules/_testcapimodule.c 2009-12-20 21:30:08.584652647 -0800 @@ -357,6 +357,57 @@ #undef F_U_TO_PY #undef F_PY_TO_U +/* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG + is tested by test_long_api_inner. This test will concentrate on proper + handling of overflow. +*/ + +static PyObject * +test_long_and_overflow(PyObject *self) +{ + PyObject *num; + long value; + int overflow; + + /* a number larger than LONG_MAX even on 64-bit platforms */ + num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); + if (num == NULL) + return NULL; + + /* Test that overflow is set properly for a large value. */ + overflow = 1234; + value = PyLong_AsLongAndOverflow(num, &overflow); + if (!overflow) + return raiseTestError("test_long_and_overflow", + "overflow was not set"); + + overflow = 0; + value = PyLong_AsLongAndOverflow(num, &overflow); + if (!overflow) + return raiseTestError("test_long_and_overflow", + "overflow was not set"); + + num = PyLong_FromString("FF", NULL, 16); + if (num == NULL) + return NULL; + + /* Test that overflow is cleared properly for a small value. */ + overflow = 1234; + value = PyLong_AsLongAndOverflow(num, &overflow); + if (overflow) + return raiseTestError("test_long_and_overflow", + "overflow was not cleared"); + + overflow = 0; + value = PyLong_AsLongAndOverflow(num, &overflow); + if (overflow) + return raiseTestError("test_long_and_overflow", + "overflow was set incorrectly"); + + Py_INCREF(Py_None); + return Py_None; +} + /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG for both long and int arguments. The test may leak a little memory if it fails. @@ -1016,6 +1067,7 @@ {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, + {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, METH_NOARGS}, {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, --- ./Doc/c-api/long-orig.rst 2009-12-20 21:38:06.990284304 -0800 +++ ./Doc/c-api/long.rst 2009-12-20 21:41:24.050259078 -0800 @@ -133,6 +133,16 @@ and ``-1`` will be returned. +.. cfunction:: long PyLong_AsLongAndOverflow(PyObject *pylong, int* overflow) + + Return a C :ctype:`long` representation of the contents of *pylong*. If + *pylong* is greater than :const:`LONG_MAX`, return -1 and + set `*overflow` to 1 (for overflow) or -1 (for underflow). + If an exception is set because of type errors, also return -1. + + .. versionadded:: 2.7 + + .. cfunction:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong) .. index::