]> granicus.if.org Git - python/commitdiff
Merged revisions 69498 via svnmerge from
authorMark Dickinson <dickinsm@gmail.com>
Tue, 10 Feb 2009 16:13:25 +0000 (16:13 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 10 Feb 2009 16:13:25 +0000 (16:13 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r69498 | mark.dickinson | 2009-02-10 15:46:50 +0000 (Tue, 10 Feb 2009) | 6 lines

  Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError for
  negative arguments.  Previously, it raised TypeError.

  Thanks Lisandro Dalcin.
........

Doc/c-api/long.rst
Lib/test/test_struct.py
Misc/ACKS
Misc/NEWS
Modules/testcapi_long.h
Objects/longobject.c

index a7fe4b5547b7085401d7e48b6f773c31002cbc6e..a8dc3eccbc9a138594504b384fc9e199a7892ab8 100644 (file)
@@ -168,17 +168,26 @@ All integers are implemented as "long" integer objects of arbitrary size.
 
 .. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
 
-   Return a C :ctype:`long long` from a Python integer.  If *pylong* cannot be
-   represented as a :ctype:`long long`, an :exc:`OverflowError` will be raised.
+   .. index::
+      single: OverflowError (built-in exception)
 
+   Return a C :ctype:`long long` from a Python integer.  If *pylong*
+   cannot be represented as a :ctype:`long long`, an
+   :exc:`OverflowError` is raised and ``-1`` is returned.
 
 .. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
 
-   Return a C :ctype:`unsigned long long` from a Python 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 integer. If
+   *pylong* cannot be represented as an :ctype:`unsigned long long`,
+   an :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
+   returned.
 
+   .. versionchanged:: 3.1
+      A negative *pylong* now raises :exc:`OverflowError`, not
+      :exc:`TypeError`.
 
 .. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
 
index bdbc397415cbc23b7092b56fb4e0fbf9fbef9d74..2bc92f3b394357f6a74d668f0389a440e793d3b2 100644 (file)
@@ -48,7 +48,7 @@ def with_warning_restore(func):
 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:
@@ -191,7 +191,7 @@ class StructTest(unittest.TestCase):
 
     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 31f920e54ea148d560ed8d1f4084d0a260660ab5..669c4e0d45d8c88fdcf68055e3896afd375d66c7 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -152,6 +152,7 @@ Simon Cross
 Drew Csillag
 John Cugini
 Tom Culliton
+Lisandro Dalcin
 Andrew Dalke
 Lars Damerow
 Eric Daniel
index 5970332dac232d927c7e6030374e2651327a9bd6..44a893ff0ecd59a85b809d993eaa882ce672cf5f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -467,6 +467,9 @@ Build
 C-API
 -----
 
+- Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError
+  for negative arguments.  Previously, it raised TypeError.
+
 - Issue #4720: The format for PyArg_ParseTupleAndKeywords can begin with '|'.
 
 - Issue #3632: from the gdb debugger, the 'pyo' macro can now be called when
index 8ed6b021ea45ef546ce65e593b6ebd68950d29f4..60ca326a79713195585fe4e45ada4b4d62146c28 100644 (file)
@@ -97,6 +97,10 @@ TESTNAME(PyObject *error(const char*))
                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);
 
@@ -112,11 +116,15 @@ TESTNAME(PyObject *error(const char*))
                        return error(
                                "unexpected NULL from PyNumber_Lshift");
 
-               uout = F_PY_TO_U(x);
+               uout = F_PY_TO_U(x);
                if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
                        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 @@ TESTNAME(PyObject *error(const char*))
                        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 @@ TESTNAME(PyObject *error(const char*))
                        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);
 
index e8d315def67fef43b52a50dd52e6c8dd18798283..e2ab078b9061afe393bfebfb43b4010586717782 100644 (file)
@@ -786,7 +786,7 @@ _PyLong_AsByteArray(PyLongObject* v,
        if (Py_SIZE(v) < 0) {
                ndigits = -(Py_SIZE(v));
                if (!is_signed) {
-                       PyErr_SetString(PyExc_TypeError,
+                       PyErr_SetString(PyExc_OverflowError,
                                "can't convert negative int to unsigned");
                        return -1;
                }