diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index ea1e2d4cb..64516184c 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -2049,7 +2049,6 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase): self.assertEqual(b, b"") self.assertEqual(c, b"") - @unittest.expectedFailure # TODO: RUSTPYTHON; BufferError: Existing exports of data: object cannot be re-sized def test_resize_forbidden(self): # #4509: can't resize a bytearray when there are buffer exports, even # if it wouldn't reallocate the underlying buffer. diff --git a/crates/vm/src/builtins/bytearray.rs b/crates/vm/src/builtins/bytearray.rs index e723449c8..3ac471c0f 100644 --- a/crates/vm/src/builtins/bytearray.rs +++ b/crates/vm/src/builtins/bytearray.rs @@ -1043,8 +1043,19 @@ impl Py { if size < 0 { return Err(vm.new_value_error("bytearray.resize(): new size must be >= 0")); } - let mut inner = self.try_resizable(vm)?; let size = size as usize; + // An export's stable length avoids re-entering the payload lock. + let access = self.resize_access(); + let len = match &access { + ResizeAccess::Exclusive(inner) => inner.elements.len(), + ResizeAccess::Exported { len } => *len, + }; + if size == len { + return Ok(()); + } + let ResizeAccess::Exclusive(mut inner) = access else { + return Err(vm.new_buffer_error("Existing exports of data: object cannot be re-sized")); + }; let elements = &mut inner.elements; elements .try_reserve_exact(size.saturating_sub(elements.len()))