diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index bb2431d72..2675507e3 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -2,7 +2,7 @@ /// [https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting] use crate::format::get_num_digits; use crate::obj::{objfloat, objint, objstr, objtuple, objtype}; -use crate::pyobject::{PyObjectRef, PyResult, TryFromObject, TypeProtocol}; +use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult, TryFromObject, TypeProtocol}; use crate::vm::VirtualMachine; use num_bigint::{BigInt, Sign}; use num_traits::cast::ToPrimitive; @@ -407,14 +407,6 @@ impl CFormatString { vm: &VirtualMachine, values_obj: PyObjectRef, ) -> PyResult { - fn call_getitem( - vm: &VirtualMachine, - container: &PyObjectRef, - key: &PyObjectRef, - ) -> PyResult { - vm.call_method(container, "__getitem__", vec![key.clone()]) - } - fn try_update_quantity_from_tuple( vm: &VirtualMachine, elements: &mut dyn Iterator, @@ -494,7 +486,7 @@ impl CFormatString { let obj: PyObjectRef = match &format_spec.mapping_key { Some(key) => { // TODO: change the KeyError message to match the one in cpython - call_getitem(vm, &values, &vm.ctx.new_str(key.to_owned()))? + values.get_item(key, vm)? } None => { let mut elements = objtuple::get_value(&values) diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs index 12f3c2637..afdfbb131 100644 --- a/vm/src/obj/objmemory.rs +++ b/vm/src/obj/objmemory.rs @@ -1,7 +1,7 @@ use super::objbyteinner::try_as_byte; use super::objtype::{issubclass, PyClassRef}; use crate::pyobject::{ - PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, + ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, }; use crate::stdlib::array::PyArray; use crate::vm::VirtualMachine; @@ -57,7 +57,7 @@ impl PyMemoryView { #[pymethod(name = "__getitem__")] fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { - vm.call_method(&self.obj_ref, "__getitem__", vec![needle]) + self.obj_ref.get_item(&needle, vm) } #[pymethod(magic)]