Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce071ca

Browse files
committed
bytes should be verboten in sum() (fixes #12654)
1 parent e12c0b1 commit ce071ca

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ def test_sum(self):
11281128
self.assertRaises(TypeError, sum, 42)
11291129
self.assertRaises(TypeError, sum, ['a', 'b', 'c'])
11301130
self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '')
1131+
self.assertRaises(TypeError, sum, [b'a', b'c'], b'')
1132+
values = [bytearray(b'a'), bytearray(b'b')]
1133+
self.assertRaises(TypeError, sum, values, bytearray(b''))
11311134
self.assertRaises(TypeError, sum, [[1], [2], [3]])
11321135
self.assertRaises(TypeError, sum, [{2:3}])
11331136
self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3})

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Forbid summing bytes in sum().
14+
1315
- Verify the types of AST strings and identifiers provided by the user before
1416
compiling them.
1517

Python/bltinmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,11 @@ builtin_sum(PyObject *self, PyObject *args)
18881888
Py_DECREF(iter);
18891889
return NULL;
18901890
}
1891+
if (PyBytes_Check(result)) {
1892+
PyErr_SetString(PyExc_TypeError,
1893+
"sum() can't sum bytes [use b''.join(seq) instead]");
1894+
return NULL;
1895+
}
18911896
if (PyByteArray_Check(result)) {
18921897
PyErr_SetString(PyExc_TypeError,
18931898
"sum() can't sum bytes [use b''.join(seq) instead]");

0 commit comments

Comments
 (0)