Kaydet (Commit) c742dff1 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #27039: Fixed bytearray.remove() for values greater than 127.

Patch by Joe Jevnik.
üst 3079bbeb
...@@ -723,6 +723,13 @@ class ByteArrayTest(BaseBytesTest): ...@@ -723,6 +723,13 @@ class ByteArrayTest(BaseBytesTest):
b.remove(Indexable(ord('e'))) b.remove(Indexable(ord('e')))
self.assertEqual(b, b'') 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): def test_pop(self):
b = bytearray(b'world') b = bytearray(b'world')
self.assertEqual(b.pop(), ord('d')) self.assertEqual(b.pop(), ord('d'))
......
...@@ -652,6 +652,7 @@ Philip Jenvey ...@@ -652,6 +652,7 @@ Philip Jenvey
MunSic Jeong MunSic Jeong
Chris Jerdonek Chris Jerdonek
Dmitry Jeremov Dmitry Jeremov
Joe Jevnik
Jim Jewett Jim Jewett
Pedro Diaz Jimenez Pedro Diaz Jimenez
Orjan Johansen Orjan Johansen
......
...@@ -10,6 +10,9 @@ What's New in Python 2.7.12? ...@@ -10,6 +10,9 @@ What's New in Python 2.7.12?
Core and Builtins 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 - Issue #4806: Avoid masking the original TypeError exception when using star
(*) unpacking and the exception was raised from a generator. Based on (*) unpacking and the exception was raised from a generator. Based on
patch by Hagen Fürstenau. patch by Hagen Fürstenau.
......
...@@ -2395,23 +2395,21 @@ static PyObject * ...@@ -2395,23 +2395,21 @@ static PyObject *
bytearray_remove(PyByteArrayObject *self, PyObject *arg) bytearray_remove(PyByteArrayObject *self, PyObject *arg)
{ {
int value; int value;
Py_ssize_t where, n = Py_SIZE(self); Py_ssize_t n = Py_SIZE(self);
char *where;
if (! _getbytevalue(arg, &value)) if (! _getbytevalue(arg, &value))
return NULL; return NULL;
for (where = 0; where < n; where++) { where = memchr(self->ob_bytes, value, n);
if (self->ob_bytes[where] == value) if (!where) {
break;
}
if (where == n) {
PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
return NULL; return NULL;
} }
if (!_canresize(self)) if (!_canresize(self))
return NULL; 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) if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
return NULL; return NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment