]> granicus.if.org Git - python/commitdiff
Merged revisions 80125,80128,80130 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Fri, 16 Apr 2010 23:19:11 +0000 (23:19 +0000)
committerBenjamin Peterson <benjamin@python.org>
Fri, 16 Apr 2010 23:19:11 +0000 (23:19 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r80125 | benjamin.peterson | 2010-04-16 17:35:32 -0500 (Fri, 16 Apr 2010) | 13 lines

  Merged revisions 80123-80124 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r80123 | benjamin.peterson | 2010-04-16 17:24:16 -0500 (Fri, 16 Apr 2010) | 1 line

    bytearray -> type2test
  ........
    r80124 | benjamin.peterson | 2010-04-16 17:25:57 -0500 (Fri, 16 Apr 2010) | 1 line

    fix typo
  ........
................
  r80128 | benjamin.peterson | 2010-04-16 17:51:37 -0500 (Fri, 16 Apr 2010) | 9 lines

  Merged revisions 80126 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r80126 | benjamin.peterson | 2010-04-16 17:35:38 -0500 (Fri, 16 Apr 2010) | 1 line

    have a clear error when passing something > sys.maxsize to bytearray
  ........
................
  r80130 | benjamin.peterson | 2010-04-16 18:00:53 -0500 (Fri, 16 Apr 2010) | 9 lines

  Merged revisions 80129 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r80129 | benjamin.peterson | 2010-04-16 17:52:44 -0500 (Fri, 16 Apr 2010) | 1 line

    tiny simplification
  ........
................

Lib/test/test_bytes.py
Misc/NEWS
Objects/bytearrayobject.c
Objects/bytesobject.c

index b83a83fd4b255c560165797ff19cf636df19fb6c..28ae5cac263aa8a8cf7ee4290d8229b61dbd6fb4 100644 (file)
@@ -63,17 +63,18 @@ class BaseBytesTest(unittest.TestCase):
         b = self.type2test([Indexable(), Indexable(1), Indexable(254),
                             Indexable(255)])
         self.assertEqual(list(b), [0, 1, 254, 255])
-        self.assertRaises(ValueError, bytearray, [Indexable(-1)])
-        self.assertRaises(ValueError, bytearray, [Indexable(256)])
+        self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
+        self.assertRaises(ValueError, self.type2test, [Indexable(256)])
 
     def test_from_ssize(self):
-        self.assertEqual(bytearray(0), b'')
-        self.assertEqual(bytearray(1), b'\x00')
-        self.assertEqual(bytearray(5), b'\x00\x00\x00\x00\x00')
-        self.assertRaises(ValueError, bytearray, -1)
-
-        self.assertEqual(bytearray('0', 'ascii'), b'0')
-        self.assertEqual(bytearray(b'0'), b'0')
+        self.assertEqual(self.type2test(0), b'')
+        self.assertEqual(self.type2test(1), b'\x00')
+        self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00')
+        self.assertRaises(ValueError, self.type2test, -1)
+
+        self.assertEqual(self.type2test('0', 'ascii'), b'0')
+        self.assertEqual(self.type2test(b'0'), b'0')
+        self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1)
 
     def test_constructor_type_errors(self):
         self.assertRaises(TypeError, self.type2test, 0.0)
index 1ed921c02871bdbdb36a39f2ecfcc2c84236605a..ab662cbc7a55dd7d6c00fe85848a589dfa297802 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@ Core and Builtins
 - Issue #8014: Setting a T_UINT or T_PYSSIZET attribute of an object with
   PyMemberDefs could produce an internal error;  raise TypeError instead.
 
+- Issue #8417: Raise an OverflowError when an integer larger than sys.maxsize is
+  passed to bytes or bytearray.
+
 - Issue #8329: Don't return the same lists from select.select when no fds are
   changed.
 
index d25144d617593e5f4f4c13f947e80d6212d8fd6f..18e6f8d8ec27a5d6d5f4cde85d0be872e366f49d 100644 (file)
@@ -761,14 +761,17 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
     }
 
     /* Is it an int? */
-    count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
-    if (count == -1 && PyErr_Occurred())
+    count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
+    if (count == -1 && PyErr_Occurred()) {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            return -1;
         PyErr_Clear();
+    }
+    else if (count < 0) {
+        PyErr_SetString(PyExc_ValueError, "negative count");
+        return -1;
+    }
     else {
-        if (count < 0) {
-            PyErr_SetString(PyExc_ValueError, "negative count");
-            return -1;
-        }
         if (count > 0) {
             if (PyByteArray_Resize((PyObject *)self, count))
                 return -1;
index 6584462e4dd2b3a5a20871c47f1f69399ee34f39..36d71c0e490f408c040157f0c502e62b50591d37 100644 (file)
@@ -2949,15 +2949,17 @@ PyBytes_FromObject(PyObject *x)
        }
 
        /* Is it an int? */
-       size = PyNumber_AsSsize_t(x, PyExc_ValueError);
+       size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
        if (size == -1 && PyErr_Occurred()) {
+               if (PyErr_ExceptionMatches(PyExc_OverflowError))
+                       return NULL;
                PyErr_Clear();
        }
+       else if (size < 0) {
+               PyErr_SetString(PyExc_ValueError, "negative count");
+               return NULL;
+       }
        else {
-               if (size < 0) {
-                       PyErr_SetString(PyExc_ValueError, "negative count");
-                       return NULL;
-               }
                new = PyBytes_FromStringAndSize(NULL, size);
                if (new == NULL) {
                        return NULL;