]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27039: Fixed bytearray.remove() for values greater than 127.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 16 May 2016 19:15:57 +0000 (22:15 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 16 May 2016 19:15:57 +0000 (22:15 +0300)
Patch by Joe Jevnik.

Lib/test/test_bytes.py
Misc/ACKS
Misc/NEWS
Objects/bytearrayobject.c

index 8e33b52c9b78fef226ca51177cc9ac61541df129..4a70b33bc0b9e37b4ba377c551f3a33e6f7a2140 100644 (file)
@@ -723,6 +723,13 @@ class ByteArrayTest(BaseBytesTest):
         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, bytearray([126, 128, 129]))
+        c.remove(129)
+        self.assertEqual(c, bytearray([126, 128]))
+
     def test_pop(self):
         b = bytearray(b'world')
         self.assertEqual(b.pop(), ord('d'))
index eaee3343c84d6d3d4d609f7550596632768a4730..df9ee4a4bd519c0677e2814cc06af6c0be94b542 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -652,6 +652,7 @@ Philip Jenvey
 MunSic Jeong
 Chris Jerdonek
 Dmitry Jeremov
+Joe Jevnik
 Jim Jewett
 Pedro Diaz Jimenez
 Orjan Johansen
index 342540336937d1b0cfe01acb2614e709d6051791..e825a2ee9e688ad799c1c0c0ea6fe8a1cd00c447 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 2.7.12?
 Core and Builtins
 -----------------
 
+- Issue #27039: Fixed bytearray.remove() for values greater than 127.  Patch by
+  Joe Jevnik.
+
 - Issue #4806: Avoid masking the original TypeError exception when using star
   (*) unpacking and the exception was raised from a generator.  Based on
   patch by Hagen Fürstenau.
index 3db35917026db726f70129f64f787e26ce784880..a90bdebb162c1c3dfa317397f6b168ad68ae66e4 100644 (file)
@@ -2395,23 +2395,21 @@ static PyObject *
 bytearray_remove(PyByteArrayObject *self, PyObject *arg)
 {
     int value;
-    Py_ssize_t where, n = Py_SIZE(self);
+    Py_ssize_t n = Py_SIZE(self);
+    char *where;
 
     if (! _getbytevalue(arg, &value))
         return NULL;
 
-    for (where = 0; where < n; where++) {
-        if (self->ob_bytes[where] == value)
-            break;
-    }
-    if (where == n) {
+    where = memchr(self->ob_bytes, value, n);
+    if (!where) {
         PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
         return NULL;
     }
     if (!_canresize(self))
         return NULL;
 
-    memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
+    memmove(where, where + 1, self->ob_bytes + n - where);
     if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
         return NULL;