[Python-checkins] r80126 - in python/trunk: Lib/test/test_bytes.py Misc/NEWS Objects/bytearrayobject.c

benjamin.peterson python-checkins at python.org
Sat Apr 17 00:35:38 CEST 2010


Author: benjamin.peterson
Date: Sat Apr 17 00:35:38 2010
New Revision: 80126

Log:
have a clear error when passing something > sys.maxsize to bytearray

Modified:
   python/trunk/Lib/test/test_bytes.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/bytearrayobject.c

Modified: python/trunk/Lib/test/test_bytes.py
==============================================================================
--- python/trunk/Lib/test/test_bytes.py	(original)
+++ python/trunk/Lib/test/test_bytes.py	Sat Apr 17 00:35:38 2010
@@ -73,6 +73,7 @@
 
         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)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Apr 17 00:35:38 2010
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #8417: Raise an OverflowError when an integer larger than sys.maxsize is
+  passed to bytearray.
+
 Library
 -------
 

Modified: python/trunk/Objects/bytearrayobject.c
==============================================================================
--- python/trunk/Objects/bytearrayobject.c	(original)
+++ python/trunk/Objects/bytearrayobject.c	Sat Apr 17 00:35:38 2010
@@ -824,14 +824,18 @@
     }
 
     /* Is it an int? */
-    count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
-    if (count == -1 && PyErr_Occurred())
-        PyErr_Clear();
-    else {
-        if (count < 0) {
-            PyErr_SetString(PyExc_ValueError, "negative count");
+    count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
+    if (count == -1 && PyErr_Occurred()) {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
             return -1;
-        }
+        else
+            PyErr_Clear();
+    }
+    else if (count < 0) {
+        PyErr_SetString(PyExc_ValueError, "negative count");
+        return -1;
+    }
+    else {
         if (count > 0) {
             if (PyByteArray_Resize((PyObject *)self, count))
                 return -1;


More information about the Python-checkins mailing list