[Python-checkins] cpython (3.5): Issue #27039: Fixed bytearray.remove() for values greater than 127.
serhiy.storchaka
python-checkins at python.org
Mon May 16 15:24:37 EDT 2016
https://hg.python.org/cpython/rev/10444778d41c
changeset: 101377:10444778d41c
branch: 3.5
parent: 101375:6c433a669e67
user: Serhiy Storchaka <storchaka at gmail.com>
date: Mon May 16 22:15:38 2016 +0300
summary:
Issue #27039: Fixed bytearray.remove() for values greater than 127.
Patch by Joe Jevnik.
files:
Lib/test/test_bytes.py | 7 +++++++
Misc/NEWS | 3 +++
Objects/bytearrayobject.c | 13 +++++--------
3 files changed, 15 insertions(+), 8 deletions(-)
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
@@ -1082,6 +1082,13 @@
b.remove(Indexable(ord('e')))
self.assertEqual(b, b'')
+ # test values outside of the ascii range: (0, 127)
+ c = bytearray([126, 127, 128, 129])
+ c.remove(127)
+ self.assertEqual(c, bytes([126, 128, 129]))
+ c.remove(129)
+ self.assertEqual(c, bytes([126, 128]))
+
def test_pop(self):
b = bytearray(b'world')
self.assertEqual(b.pop(), ord('d'))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #27039: Fixed bytearray.remove() for values greater than 127. Patch by
+ Joe Jevnik.
+
- Issue #23640: int.from_bytes() no longer bypasses constructors for subclasses.
- Issue #26811: gc.get_objects() no longer contains a broken tuple with NULL
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2565,21 +2565,18 @@
bytearray_remove_impl(PyByteArrayObject *self, int value)
/*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/
{
- Py_ssize_t where, n = Py_SIZE(self);
+ Py_ssize_t n = Py_SIZE(self);
char *buf = PyByteArray_AS_STRING(self);
-
- for (where = 0; where < n; where++) {
- if (buf[where] == value)
- break;
- }
- if (where == n) {
+ char *where = memchr(buf, value, n);
+
+ if (!where) {
PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
return NULL;
}
if (!_canresize(self))
return NULL;
- memmove(buf + where, buf + where + 1, n - where);
+ memmove(where, where + 1, buf + n - where);
if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
return NULL;
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list