]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bytes should be verboten in sum() (fixes #12654)
authorBenjamin Peterson <benjamin@python.org>
Fri, 29 Jul 2011 19:23:47 +0000 (14:23 -0500)
committerBenjamin Peterson <benjamin@python.org>
Fri, 29 Jul 2011 19:23:47 +0000 (14:23 -0500)
Lib/test/test_builtin.py
Misc/NEWS
Python/bltinmodule.c

index ce1586f98d8647cd31f6015b826c990973c0b661..aa9b4e2ed8d69221df61de2e25c8612f0c371572 100644 (file)
@@ -1128,6 +1128,9 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(TypeError, sum, 42)
         self.assertRaises(TypeError, sum, ['a', 'b', 'c'])
         self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '')
+        self.assertRaises(TypeError, sum, [b'a', b'c'], b'')
+        values = [bytearray(b'a'), bytearray(b'b')]
+        self.assertRaises(TypeError, sum, values, bytearray(b''))
         self.assertRaises(TypeError, sum, [[1], [2], [3]])
         self.assertRaises(TypeError, sum, [{2:3}])
         self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3})
index 5cdcb79cba0d0537be05467f17616104380811e2..0b205ad9a8b3b54f94ffef385529cccb6bfcb487 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1?
 Core and Builtins
 -----------------
 
+- Forbid summing bytes in sum().
+
 - Verify the types of AST strings and identifiers provided by the user before
   compiling them.
 
index 291ef45e67c7d629bfcf2d99ac4a1e9bf414a187..82fb9ad1651846ded64c3c32cb7af16e0a725e37 100644 (file)
@@ -1888,6 +1888,11 @@ builtin_sum(PyObject *self, PyObject *args)
             Py_DECREF(iter);
             return NULL;
         }
+        if (PyBytes_Check(result)) {
+            PyErr_SetString(PyExc_TypeError,
+                "sum() can't sum bytes [use b''.join(seq) instead]");
+            return NULL;
+        }
         if (PyByteArray_Check(result)) {
             PyErr_SetString(PyExc_TypeError,
                 "sum() can't sum bytes [use b''.join(seq) instead]");