Skip to content

Commit 43cceb1

Browse files
committed
switch SequenceIndex to PyObjectRef
As `getitem` and `setitem methods in other types, get second argument as PyObjectRef.
1 parent 4e63557 commit 43cceb1

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

vm/src/builtins/memory.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::slots::{AsBuffer, AsMapping, Comparable, Hashable, PyComparisonOp, Sl
1515
use crate::stdlib::pystruct::_struct::FormatSpec;
1616
use crate::utils::Either;
1717
use crate::{
18-
IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
19-
PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
18+
IdProtocol, IntoPyObject, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
19+
PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
2020
};
2121
use crossbeam_utils::atomic::AtomicCell;
2222
use itertools::Itertools;
@@ -345,10 +345,10 @@ impl PyMemoryView {
345345
}
346346

347347
#[pymethod(magic)]
348-
fn getitem(zelf: PyRef<Self>, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
348+
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
349349
zelf.try_not_released(vm)?;
350-
match needle {
351-
SequenceIndex::Int(i) => Self::getitem_by_idx(zelf, i, vm),
350+
match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? {
351+
SequenceIndex::Int(index) => Self::getitem_by_idx(zelf, index, vm),
352352
SequenceIndex::Slice(slice) => Self::getitem_by_slice(zelf, slice, vm),
353353
}
354354
}
@@ -462,16 +462,16 @@ impl PyMemoryView {
462462
#[pymethod(magic)]
463463
fn setitem(
464464
zelf: PyRef<Self>,
465-
needle: SequenceIndex,
465+
needle: PyObjectRef,
466466
value: PyObjectRef,
467467
vm: &VirtualMachine,
468468
) -> PyResult<()> {
469469
zelf.try_not_released(vm)?;
470470
if zelf.buffer.options.readonly {
471471
return Err(vm.new_type_error("cannot modify read-only memory".to_owned()));
472472
}
473-
match needle {
474-
SequenceIndex::Int(i) => Self::setitem_by_idx(zelf, i, value, vm),
473+
match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? {
474+
SequenceIndex::Int(index) => Self::setitem_by_idx(zelf, index, value, vm),
475475
SequenceIndex::Slice(slice) => Self::setitem_by_slice(zelf, slice, value, vm),
476476
}
477477
}
@@ -753,8 +753,7 @@ impl AsMapping for PyMemoryView {
753753

754754
#[inline]
755755
fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
756-
Self::downcast(zelf, vm)
757-
.map(|zelf| Self::getitem(zelf, SequenceIndex::try_from_object(vm, needle)?, vm))?
756+
Self::downcast(zelf, vm).map(|zelf| Self::getitem(zelf, needle, vm))?
758757
}
759758

760759
#[inline]
@@ -764,9 +763,7 @@ impl AsMapping for PyMemoryView {
764763
value: PyObjectRef,
765764
vm: &VirtualMachine,
766765
) -> PyResult<()> {
767-
Self::downcast(zelf, vm).map(|zelf| {
768-
Self::setitem(zelf, SequenceIndex::try_from_object(vm, needle)?, value, vm)
769-
})?
766+
Self::downcast(zelf, vm).map(|zelf| Self::setitem(zelf, needle, value, vm))?
770767
}
771768
}
772769

0 commit comments

Comments
 (0)