From: Benjamin Peterson Date: Fri, 29 Jul 2011 19:23:47 +0000 (-0500) Subject: bytes should be verboten in sum() (fixes #12654) X-Git-Tag: v3.3.0a1~1812 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ce071ca4e76b67060f55c6ed6fd9a7433cb66013;p=thirdparty%2FPython%2Fcpython.git bytes should be verboten in sum() (fixes #12654) --- diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index ce1586f98d86..aa9b4e2ed8d6 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -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}) diff --git a/Misc/NEWS b/Misc/NEWS index 5cdcb79cba0d..0b205ad9a8b3 100644 --- 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. diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 291ef45e67c7..82fb9ad16518 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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]");