Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b23494

Browse files
Issue #27039: Fixed bytearray.remove() for values greater than 127.
Based on patch by Joe Jevnik.
2 parents aa99b8e + 7bf36da commit 4b23494

4 files changed

Lines changed: 115 additions & 108 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,13 @@ def test_remove(self):
11771177
b.remove(Indexable(ord('e')))
11781178
self.assertEqual(b, b'')
11791179

1180+
# test values outside of the ascii range: (0, 127)
1181+
c = bytearray([126, 127, 128, 129])
1182+
c.remove(127)
1183+
self.assertEqual(c, bytes([126, 128, 129]))
1184+
c.remove(129)
1185+
self.assertEqual(c, bytes([126, 128]))
1186+
11801187
def test_pop(self):
11811188
b = bytearray(b'world')
11821189
self.assertEqual(b.pop(), ord('d'))

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #27039: Fixed bytearray.remove() for values greater than 127. Based on
14+
patch by Joe Jevnik.
15+
1316
- Issue #23640: int.from_bytes() no longer bypasses constructors for subclasses.
1417

1518
- Issue #27005: Optimized the float.fromhex() class method for exact float.

Objects/bytearrayobject.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,11 +1734,8 @@ bytearray_remove_impl(PyByteArrayObject *self, int value)
17341734
Py_ssize_t where, n = Py_SIZE(self);
17351735
char *buf = PyByteArray_AS_STRING(self);
17361736

1737-
for (where = 0; where < n; where++) {
1738-
if (buf[where] == value)
1739-
break;
1740-
}
1741-
if (where == n) {
1737+
where = stringlib_find_char(buf, n, value);
1738+
if (where < 0) {
17421739
PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
17431740
return NULL;
17441741
}

0 commit comments

Comments
 (0)