diff -r 0fe913de4702 Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py Mon May 16 09:35:18 2016 -0700 +++ b/Lib/test/test_bytes.py Mon May 16 13:40:02 2016 -0400 @@ -1177,6 +1177,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 -r 0fe913de4702 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Mon May 16 09:35:18 2016 -0700 +++ b/Objects/bytearrayobject.c Mon May 16 13:40:02 2016 -0400 @@ -1731,20 +1731,17 @@ bytearray_remove_impl(PyByteArrayObject *self, int value) /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ { - Py_ssize_t where, n = Py_SIZE(self); + Py_ssize_t n = Py_SIZE(self); char *buf = PyByteArray_AS_STRING(self); + char *where = memchr(buf, value, n); - for (where = 0; where < n; where++) { - if (buf[where] == value) - break; - } - if (where == 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;