mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
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:
1 parent
ccab27c12a
commit
e57c8db83a
2 files changed
+12
-2
No files matched your search
Vendored
-1
@@ -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.
|
||||
|
||||
@@ -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()))
|
||||
|
||||
Reference in new issue
Block a user