Index: Objects/longobject.c =================================================================== --- Objects/longobject.c (revision 69488) +++ Objects/longobject.c (working copy) @@ -557,7 +557,7 @@ if (Py_SIZE(v) < 0) { ndigits = -(Py_SIZE(v)); if (!is_signed) { - PyErr_SetString(PyExc_TypeError, + PyErr_SetString(PyExc_OverflowError, "can't convert negative long to unsigned"); return -1; } Index: Doc/c-api/long.rst =================================================================== --- Doc/c-api/long.rst (revision 69488) +++ Doc/c-api/long.rst (working copy) @@ -155,22 +155,32 @@ .. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong) + .. index:: + single: OverflowError (built-in exception) + Return a C :ctype:`long long` from a Python long integer. If *pylong* cannot be - represented as a :ctype:`long long`, an :exc:`OverflowError` will be raised. + represented as a :ctype:`long long`, an :exc:`OverflowError` is raised and + ``-1`` is returned. .. versionadded:: 2.2 .. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong) - Return a C :ctype:`unsigned long long` from a Python long integer. If *pylong* - cannot be represented as an :ctype:`unsigned long long`, an :exc:`OverflowError` - will be raised if the value is positive, or a :exc:`TypeError` will be raised if - the value is negative. + .. index:: + single: OverflowError (built-in exception) + Return a C :ctype:`unsigned long long` from a Python long integer. If + *pylong* cannot be represented as an :ctype:`unsigned long long`, an + :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is + returned. + .. versionadded:: 2.2 + .. versionchanged:: 2.7 + A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`. + .. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io) Return a C :ctype:`unsigned long` from a Python long integer, without checking Index: Lib/test/test_struct.py =================================================================== --- Lib/test/test_struct.py (revision 69488) +++ Lib/test/test_struct.py (working copy) @@ -48,7 +48,7 @@ def deprecated_err(func, *args): try: func(*args) - except (struct.error, TypeError): + except (struct.error, OverflowError): pass except DeprecationWarning: if not PY_STRUCT_OVERFLOW_MASKING: @@ -185,7 +185,7 @@ def test_native_qQ(self): # can't pack -1 as unsigned regardless - self.assertRaises((struct.error, TypeError), struct.pack, "Q", -1) + self.assertRaises((struct.error, OverflowError), struct.pack, "Q", -1) # can't pack string as 'q' regardless self.assertRaises(struct.error, struct.pack, "q", "a") # ditto, but 'Q' Index: Modules/testcapi_long.h =================================================================== --- Modules/testcapi_long.h (revision 69488) +++ Modules/testcapi_long.h (working copy) @@ -97,6 +97,10 @@ if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred()) return error( "PyLong_AsUnsignedXXX(-1) didn't complain"); + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) + return error( + "PyLong_AsUnsignedXXX(-1) raised " + "something other than OverflowError"); PyErr_Clear(); UNBIND(x); @@ -117,6 +121,10 @@ return error( "PyLong_AsUnsignedXXX(2**NBITS) didn't " "complain"); + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) + return error( + "PyLong_AsUnsignedXXX(2**NBITS) raised " + "something other than OverflowError"); PyErr_Clear(); /* Signed complains about 2**(NBITS-1)? @@ -132,6 +140,10 @@ return error( "PyLong_AsXXX(2**(NBITS-1)) didn't " "complain"); + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) + return error( + "PyLong_AsXXX(2**(NBITS-1)) raised " + "something other than OverflowError"); PyErr_Clear(); /* Signed complains about -2**(NBITS-1)-1?; @@ -153,6 +165,10 @@ return error( "PyLong_AsXXX(-2**(NBITS-1)-1) didn't " "complain"); + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) + return error( + "PyLong_AsXXX(-2**(NBITS-1)-1) raised " + "something other than OverflowError"); PyErr_Clear(); UNBIND(y);