Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
apply mapping protocol in itemprotocol
  • Loading branch information
Snowapril committed Oct 2, 2021
commit e83b68b810d66ce4837cc3fbb1e3b06e1abb709d
13 changes: 13 additions & 0 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
builtinfunc::PyNativeFuncDef,
bytearray, bytes,
code::{self, PyCode},
dict::PyMapping,
getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet},
namespace::PyNamespace,
object, pystr,
Expand Down Expand Up @@ -639,6 +640,12 @@ where
T: IntoPyObject,
{
fn get_item(&self, key: T, vm: &VirtualMachine) -> PyResult {
if let Ok(map) = PyMapping::try_from_borrowed_object(vm, self) {
if let Some(getitem) = map.subscript {
return getitem(self.clone(), key.into_pyobject(vm), vm);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need __getitem__ call under this line? I think PyMapping covers the whole cases

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. there exists extra steps (sequence protocol check & method lookup) as cpython implementation

match vm.get_special_method(self.clone(), "__getitem__")? {
Ok(special_method) => return special_method.invoke((key,), vm),
Err(obj) => {
Expand All @@ -656,6 +663,12 @@ where
}

fn set_item(&self, key: T, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
if let Ok(map) = PyMapping::try_from_borrowed_object(vm, self) {
if let Some(setitem) = map.ass_subscript {
return setitem(self.clone(), key.into_pyobject(vm), value, vm);
}
}

vm.get_special_method(self.clone(), "__setitem__")?
.map_err(|obj| {
vm.new_type_error(format!(
Expand Down