diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -885,6 +885,9 @@ b[3:0] = [42, 42, 42] self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) + with self.assertRaises(TypeError): + b[3:4] = 5 + def test_extended_set_del_slice(self): indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300) for start in indices: diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -589,6 +589,11 @@ needed = 0; } else if (values == (PyObject *)self || !PyByteArray_Check(values)) { + if(PyNumber_Check(values)) { + PyErr_SetString(PyExc_TypeError, + "can assign only bytes, buffers, and iterables"); + return -1; + } /* Make a copy and call this function recursively */ int err; values = PyByteArray_FromObject(values);