Allow same-size bytearray.resize with exported buffers (#9217)

* Allow same-size bytearray.resize with exported buffers

Use the existing export snapshot or exclusive payload access to allow
same-size requests without a recursive read lock in codec callbacks.
Continue rejecting exported length changes and preserve zero filling.

Enable the CPython regression and consolidate resize coverage in the
existing bytearray snippet, including a bounded concurrent callback test.

Assisted-by: Codex:GPT-6

* remove redundant tests
This commit is contained in:
SeungJe Jeong authored and GitHub committed 2026-10-22 19:37:21 +09:00
1 parent ccab27c12a
commit e57c8db83a
2 files changed
+12 -2

No files matched your search

-1
View File
@@ -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.
+12 -1
View File
@@ -1043,8 +1043,19 @@ impl Py<PyByteArray> {
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()))