except OSError:
pass
+ def test_mod_concurrent_mutation(self):
+ # Prevent crash in __mod__ when formatting mutates the bytearray.
+ # Regression test for https://github.com/python/cpython/issues/142557.
+ fmt = bytearray(b"%a end")
+
+ class S:
+ def __repr__(self):
+ fmt.clear()
+ return "E"
+
+ self.assertRaises(BufferError, fmt.__mod__, S())
+
def test_reverse(self):
b = bytearray(b'hello')
self.assertEqual(b.reverse(), None)
{
if (!PyByteArray_Check(v))
Py_RETURN_NOTIMPLEMENTED;
- return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
+
+ PyByteArrayObject *self = _PyByteArray_CAST(v);
+ /* Increase exports to prevent bytearray storage from changing during op. */
+ self->ob_exports++;
+ PyObject *res = _PyBytes_FormatEx(
+ PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1
+ );
+ self->ob_exports--;
+ return res;
}
static PyNumberMethods bytearray_as_number = {