diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index 9822ae078..99e20d851 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -26,8 +26,8 @@ use crate::slots::{ use crate::utils::Either; use crate::vm::VirtualMachine; use crate::{ - IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, - PyRef, PyResult, PyValue, TypeProtocol, + IdProtocol, IntoPyObject, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyIterable, + PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, }; use bstr::ByteSlice; use crossbeam_utils::atomic::AtomicCell; @@ -151,11 +151,11 @@ impl PyByteArray { #[pymethod(magic)] fn setitem( zelf: PyRef, - needle: SequenceIndex, + needle: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine, ) -> PyResult<()> { - match needle { + match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? { SequenceIndex::Int(i) => { let value = value_from_object(vm, &value)?; let mut elements = zelf.borrow_buf_mut(); @@ -192,7 +192,7 @@ impl PyByteArray { #[pymethod(magic)] fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { - self.inner().getitem("bytearray", needle, vm) + self.inner().getitem(Self::NAME, needle, vm) } #[pymethod(magic)] diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 84b3f2cde..6612f87bd 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -151,7 +151,7 @@ impl PyBytes { #[pymethod(name = "__getitem__")] fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { - self.inner.getitem("byte", needle, vm) + self.inner.getitem("byte", needle, vm) // byte != Self::NAME } #[pymethod(name = "isalnum")] diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index c26b75567..9c54227ad 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -23,8 +23,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhas use crate::utils::Either; use crate::vm::{ReprGuard, VirtualMachine}; use crate::{ - PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue, - TryFromObject, TypeProtocol, + PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, + PyResult, PyValue, TryFromObject, TypeProtocol, }; /// Built-in mutable sequence. @@ -172,7 +172,7 @@ impl PyList { #[pymethod(name = "__getitem__")] fn getitem(zelf: PyRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { - let result = match zelf.borrow_vec().get_item(vm, needle, "list")? { + let result = match zelf.borrow_vec().get_item(vm, needle, Self::NAME)? { Either::A(obj) => obj, Either::B(vec) => vm.ctx.new_list(vec), }; @@ -182,11 +182,11 @@ impl PyList { #[pymethod(name = "__setitem__")] fn setitem( &self, - subscript: SequenceIndex, + needle: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine, ) -> PyResult<()> { - match subscript { + match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? { SequenceIndex::Int(index) => self.setindex(index, value, vm), SequenceIndex::Slice(slice) => { if let Ok(sec) = PyIterable::try_from_object(vm, value) { diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 9171ad0ea..e067fa75a 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -23,8 +23,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter}; use crate::utils::Either; use crate::VirtualMachine; use crate::{ - IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyComparisonValue, PyContext, PyIterable, - PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, + IdProtocol, IntoPyObject, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, + PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, }; use rustpython_common::atomic::{self, PyAtomic, Radium}; use rustpython_common::hash; @@ -254,7 +254,7 @@ impl PyStr { #[pymethod(name = "__getitem__")] fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { - let s = match self.get_item(vm, needle, "string")? { + let s = match self.get_item(vm, needle, Self::NAME)? { Either::A(ch) => ch.to_string(), Either::B(s) => s, }; diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 312a5e885..7506cbbdf 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -19,8 +19,9 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter}; use crate::utils::Either; use crate::vm::{ReprGuard, VirtualMachine}; use crate::{ - IdProtocol, IntoPyObject, PyArithmaticValue, PyClassImpl, PyComparisonValue, PyContext, - PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject, TypeProtocol, + IdProtocol, IntoPyObject, PyArithmaticValue, PyClassDef, PyClassImpl, PyComparisonValue, + PyContext, PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject, + TypeProtocol, }; /// tuple() -> empty tuple @@ -182,7 +183,7 @@ impl PyTuple { #[pymethod(name = "__getitem__")] fn getitem(zelf: PyRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { - let result = match zelf.elements.as_ref().get_item(vm, needle, "tuple")? { + let result = match zelf.elements.as_ref().get_item(vm, needle, Self::NAME)? { Either::A(obj) => obj, Either::B(vec) => vm.ctx.new_tuple(vec), }; diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 5b9e5dfc1..13c2f4714 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -335,11 +335,11 @@ impl PyBytesInner { pub fn getitem( &self, - name: &'static str, + owner_type: &'static str, needle: PyObjectRef, vm: &VirtualMachine, ) -> PyResult { - let obj = match self.elements.get_item(vm, needle, name)? { + let obj = match self.elements.get_item(vm, needle, owner_type)? { Either::A(byte) => vm.new_pyobj(byte), Either::B(bytes) => vm.ctx.new_bytes(bytes), }; diff --git a/vm/src/sliceable.rs b/vm/src/sliceable.rs index a2c9d4f7c..c8feb83e4 100644 --- a/vm/src/sliceable.rs +++ b/vm/src/sliceable.rs @@ -336,7 +336,7 @@ pub enum SequenceIndex { } impl SequenceIndex { - fn try_from_object_for( + pub fn try_from_object_for( vm: &VirtualMachine, obj: PyObjectRef, owner_type: &'static str,