MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_item

Method get_item

crates/vm/src/protocol/object.rs:709–741  ·  view source on GitHub ↗
(&self, needle: &K, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

707 }
708
709 pub fn get_item<K: DictKey + ?Sized>(&self, needle: &K, vm: &VirtualMachine) -> PyResult {
710 if let Some(dict) = self.downcast_ref_if_exact::<PyDict>(vm) {
711 return dict.get_item(needle, vm);
712 }
713
714 let needle = needle.to_pyobject(vm);
715
716 if let Ok(mapping) = self.try_mapping(vm) {
717 mapping.subscript(&needle, vm)
718 } else if let Ok(seq) = self.try_sequence(vm) {
719 let i = needle.key_as_isize(vm)?;
720 seq.get_item(i, vm)
721 } else {
722 if self.class().fast_issubclass(vm.ctx.types.type_type) {
723 if self.is(vm.ctx.types.type_type) {
724 return PyGenericAlias::from_args(self.class().to_owned(), needle, vm)
725 .to_pyresult(vm);
726 }
727
728 if let Some(class_getitem) =
729 vm.get_attribute_opt(self.to_owned(), identifier!(vm, __class_getitem__))?
730 && !vm.is_none(&class_getitem)
731 {
732 return class_getitem.call((needle,), vm);
733 }
734 return Err(vm.new_type_error(format!(
735 "type '{}' is not subscriptable",
736 self.downcast_ref::<PyType>().unwrap().name()
737 )));
738 }
739 Err(vm.new_type_error(format!("'{}' object is not subscriptable", self.class())))
740 }
741 }
742
743 pub fn set_item<K: DictKey + ?Sized>(
744 &self,

Callers

nothing calls this directly

Calls 14

try_mappingMethod · 0.80
try_sequenceMethod · 0.80
key_as_isizeMethod · 0.80
fast_issubclassMethod · 0.80
isMethod · 0.80
get_attribute_optMethod · 0.80
ErrClass · 0.50
to_pyobjectMethod · 0.45
subscriptMethod · 0.45
classMethod · 0.45
to_pyresultMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected