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 @@ -705,6 +705,19 @@ b[3:0] = [42, 42, 42] self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) + b[3:] = b'foo' + self.assertEqual(b, bytearray([0, 1, 2, 102, 111, 111])) + + b[:3] = memoryview(b'foo') + self.assertEqual(b, bytearray([102, 111, 111, 102, 111, 111])) + + b[3:4] = [] + self.assertEqual(b, bytearray([102, 111, 111, 111, 111])) + + for elem in [5, -5, 0, 'str', 2.3]: + with self.assertRaises(TypeError): + b[3:4] = elem + 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);