Commit ce071ca4 authored by Benjamin Peterson's avatar Benjamin Peterson

bytes should be verboten in sum() (fixes #12654)

parent e12c0b17
...@@ -1128,6 +1128,9 @@ class BuiltinTest(unittest.TestCase): ...@@ -1128,6 +1128,9 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, sum, 42) self.assertRaises(TypeError, sum, 42)
self.assertRaises(TypeError, sum, ['a', 'b', 'c']) self.assertRaises(TypeError, sum, ['a', 'b', 'c'])
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, [[1], [2], [3]])
self.assertRaises(TypeError, sum, [{2:3}]) self.assertRaises(TypeError, sum, [{2:3}])
self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3}) self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3})
......
...@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1? ...@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Forbid summing bytes in sum().
- Verify the types of AST strings and identifiers provided by the user before - Verify the types of AST strings and identifiers provided by the user before
compiling them. compiling them.
......
...@@ -1888,6 +1888,11 @@ builtin_sum(PyObject *self, PyObject *args) ...@@ -1888,6 +1888,11 @@ builtin_sum(PyObject *self, PyObject *args)
Py_DECREF(iter); Py_DECREF(iter);
return NULL; 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)) { if (PyByteArray_Check(result)) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"sum() can't sum bytes [use b''.join(seq) instead]"); "sum() can't sum bytes [use b''.join(seq) instead]");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment