[Python-checkins] bpo-42924: Fix incorrect copy in bytearray_repeat (GH-24208) (#24212)
ambv
webhook-mailer at python.org
Mon Apr 26 15:40:47 EDT 2021
https://github.com/python/cpython/commit/e1203e8001432a08b87b54867490beb19a694069
commit: e1203e8001432a08b87b54867490beb19a694069
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-04-26T21:40:38+02:00
summary:
bpo-42924: Fix incorrect copy in bytearray_repeat (GH-24208) (#24212)
Before, using the * operator to repeat a bytearray would copy data from the start of
the internal buffer (ob_bytes) and not from the start of the actual data (ob_start).
(cherry picked from commit 61d8c54f43a7871d016f98b38f86858817d927d5)
Co-authored-by: Tobias Holl <TobiasHoll at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2021-01-13-14-06-01.bpo-42924._WS1Ok.rst
M Lib/test/test_bytes.py
M Objects/bytearrayobject.c
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index bbd45c75298e7..c2ec1201e8d6b 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1592,6 +1592,16 @@ def test_iterator_length_hint(self):
# Shouldn't raise an error
self.assertEqual(list(it), [])
+ def test_repeat_after_setslice(self):
+ # bpo-42924: * used to copy from the wrong memory location
+ b = bytearray(b'abc')
+ b[:2] = b'x'
+ b1 = b * 1
+ b3 = b * 3
+ self.assertEqual(b1, b'xc')
+ self.assertEqual(b1, b)
+ self.assertEqual(b3, b'xcxcxc')
+
class AssortedBytesTest(unittest.TestCase):
#
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-13-14-06-01.bpo-42924._WS1Ok.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-13-14-06-01.bpo-42924._WS1Ok.rst
new file mode 100644
index 0000000000000..33fbb5235ddb6
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-01-13-14-06-01.bpo-42924._WS1Ok.rst
@@ -0,0 +1 @@
+Fix ``bytearray`` repetition incorrectly copying data from the start of the buffer, even if the data is offset within the buffer (e.g. after reassigning a slice at the start of the ``bytearray`` to a shorter byte string).
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index d4d02336b6cf0..9812afd6f2c34 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -332,6 +332,7 @@ bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
PyByteArrayObject *result;
Py_ssize_t mysize;
Py_ssize_t size;
+ const char *buf;
if (count < 0)
count = 0;
@@ -340,13 +341,14 @@ bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
return PyErr_NoMemory();
size = mysize * count;
result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
+ buf = PyByteArray_AS_STRING(self);
if (result != NULL && size != 0) {
if (mysize == 1)
- memset(result->ob_bytes, self->ob_bytes[0], size);
+ memset(result->ob_bytes, buf[0], size);
else {
Py_ssize_t i;
for (i = 0; i < count; i++)
- memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
+ memcpy(result->ob_bytes + i*mysize, buf, mysize);
}
}
return (PyObject *)result;
More information about the Python-checkins
mailing list