mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Fix bytes stuck on setslice
This commit is contained in:
1 parent
3b7a5a3fd4
commit
c2c5a81e38
3 files changed
+23
-17
No files matched your search
@@ -1371,7 +1371,6 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
|
||||
del b[start:stop:step]
|
||||
self.assertEqual(b, bytearray(L))
|
||||
|
||||
@unittest.skip("TODO: RUSTPYTHON")
|
||||
def test_setslice_trap(self):
|
||||
# This test verifies that we correctly handle assigning self
|
||||
# to a slice of self (the old Lambert Meertens trap).
|
||||
|
||||
+10
-12
@@ -333,7 +333,12 @@ impl PyBytesInner {
|
||||
}
|
||||
}
|
||||
|
||||
fn setindex(&mut self, int: isize, object: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
pub fn setindex(
|
||||
&mut self,
|
||||
int: isize,
|
||||
object: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
if let Some(idx) = self.elements.get_pos(int) {
|
||||
let result = match_class!(match object {
|
||||
i @ PyInt => {
|
||||
@@ -353,7 +358,7 @@ impl PyBytesInner {
|
||||
}
|
||||
}
|
||||
|
||||
fn setslice(
|
||||
pub fn setslice(
|
||||
&mut self,
|
||||
slice: PySliceRef,
|
||||
object: PyObjectRef,
|
||||
@@ -379,16 +384,9 @@ impl PyBytesInner {
|
||||
self.elements.set_slice_items(vm, &slice, items.as_slice())
|
||||
}
|
||||
|
||||
pub fn setitem(
|
||||
&mut self,
|
||||
needle: SequenceIndex,
|
||||
object: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
match needle {
|
||||
SequenceIndex::Int(int) => self.setindex(int, object, vm),
|
||||
SequenceIndex::Slice(slice) => self.setslice(slice, object, vm),
|
||||
}
|
||||
pub fn setslice_from_self(&mut self, slice: PySliceRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let items = self.elements.clone();
|
||||
self.elements.set_slice_items(vm, &slice, items.as_slice())
|
||||
}
|
||||
|
||||
pub fn delitem(&mut self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult<()> {
|
||||
|
||||
@@ -16,8 +16,8 @@ use crate::bytesinner::{
|
||||
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
|
||||
use crate::function::{OptionalArg, OptionalOption};
|
||||
use crate::pyobject::{
|
||||
BorrowValue, Either, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef,
|
||||
PyResult, PyValue, TryFromObject,
|
||||
BorrowValue, Either, IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
|
||||
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
|
||||
};
|
||||
use crate::pystr::{self, PyCommonString};
|
||||
use crate::vm::VirtualMachine;
|
||||
@@ -186,12 +186,21 @@ impl PyByteArray {
|
||||
|
||||
#[pymethod(name = "__setitem__")]
|
||||
fn setitem(
|
||||
&self,
|
||||
zelf: PyRef<Self>,
|
||||
needle: SequenceIndex,
|
||||
value: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
self.borrow_value_mut().setitem(needle, value, vm)
|
||||
match needle {
|
||||
SequenceIndex::Int(int) => zelf.borrow_value_mut().setindex(int, value, vm),
|
||||
SequenceIndex::Slice(slice) => {
|
||||
if zelf.is(&value) {
|
||||
zelf.borrow_value_mut().setslice_from_self(slice, vm)
|
||||
} else {
|
||||
zelf.borrow_value_mut().setslice(slice, value, vm)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__delitem__")]
|
||||
|
||||
Reference in new issue
Block a user