[Python-checkins] cpython (2.7): Issue #27039: Fixed bytearray.remove() for values greater than 127.

serhiy.storchaka python-checkins at python.org
Mon May 16 15:24:49 EDT 2016


https://hg.python.org/cpython/rev/9acf44b7ff7b
changeset:   101378:9acf44b7ff7b
branch:      2.7
parent:      101370:789a3f87bde1
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon May 16 22:15:57 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/ACKS                 |   1 +
  Misc/NEWS                 |   3 +++
  Objects/bytearrayobject.c |  12 +++++-------
  4 files changed, 16 insertions(+), 7 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
@@ -723,6 +723,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, 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'))
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -652,6 +652,7 @@
 MunSic Jeong
 Chris Jerdonek
 Dmitry Jeremov
+Joe Jevnik
 Jim Jewett
 Pedro Diaz Jimenez
 Orjan Johansen
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 #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.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2395,23 +2395,21 @@
 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;
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list