From 06fbba6589521eb90941758969540e621eb8bfb3 Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Sun, 28 Nov 2021 12:48:19 +0200 Subject: [PATCH] clearup --- vm/src/builtins/dict.rs | 22 ++++++---------------- vm/src/frame.rs | 6 ++++-- vm/src/protocol/mapping.rs | 15 --------------- vm/src/stdlib/winapi.rs | 2 +- 4 files changed, 11 insertions(+), 34 deletions(-) diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index adc869521..d75902cbb 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -477,9 +477,10 @@ impl PyObjectView { &self, key: K, vm: &VirtualMachine, - ) -> Option { + ) -> PyResult> { vm.get_method(self.to_owned().into(), "__missing__") .map(|methods| vm.invoke(&methods?, (key,))) + .transpose() } #[inline] @@ -490,14 +491,11 @@ impl PyObjectView { ) -> PyResult { if let Some(value) = self.entries.get(vm, &key)? { Ok(value) - } else if let Some(value) = self.missing_opt(key.clone(), vm) { - value + } else if let Some(value) = self.missing_opt(key.clone(), vm)? { + Ok(value) } else { Err(vm.new_key_error(key.into_pyobject(vm))) } - // self.entries - // .get(vm, &key)? - // .ok_or_else(|| vm.new_key_error(key.into_pyobject(vm))) } /// Take a python dictionary and convert it to attributes. @@ -517,12 +515,11 @@ impl PyObjectView { ) -> PyResult> { if self.exact_dict(vm) { self.entries.get(vm, &key) + // FIXME: check __missing__? } else { match self.as_object().get_item(key.clone(), vm) { Ok(value) => Ok(Some(value)), - Err(e) if e.isinstance(&vm.ctx.exceptions.key_error) => { - self.missing_opt(key, vm).transpose() - } + Err(e) if e.isinstance(&vm.ctx.exceptions.key_error) => self.missing_opt(key, vm), Err(e) => Err(e), } } @@ -537,13 +534,6 @@ impl PyObjectView { self.inner_getitem(key, vm) } else { self.as_object().get_item(key, vm) - // match self.as_object().get_item(key.clone(), vm) { - // Ok(value) => Ok(value), - // Err(e) if e.isinstance(&vm.ctx.exceptions.key_error) => { - // self.missing_opt(key, vm).ok_or(e)? - // } - // Err(e) => Err(e), - // } } } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 2de8f8008..938257362 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -198,9 +198,11 @@ impl FrameRef { let map_to_dict = |keys: &[PyStrRef], values: &[PyCellRef]| { for (k, v) in itertools::zip(keys, values) { if let Some(value) = v.get() { - locals.as_object().set_item(k.clone(), value, vm)?; + locals + .mapping() + .ass_subscript(k.as_object(), Some(value), vm)?; } else { - match locals.as_object().del_item(k.clone(), vm) { + match locals.mapping().ass_subscript(k.as_object(), None, vm) { Ok(()) => {} Err(e) if e.isinstance(&vm.ctx.exceptions.key_error) => {} Err(e) => return Err(e), diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index 733bb5745..ef7cc3bf2 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -70,7 +70,6 @@ impl PyMapping<'_> { } pub fn methods(&self, vm: &VirtualMachine) -> &PyMappingMethods { - // let cls = self.obj.class(); self.methods.get_or_init(|| { if let Some(f) = self .obj @@ -82,20 +81,6 @@ impl PyMapping<'_> { PyMappingMethods::default() } }) - - // let get_methods = || { - // if let Some(f) = cls.mro_find_map(|cls| cls.slots.as_mapping.load()) { - // f(self.obj, vm) - // } else { - // PyMappingMethods::default() - // } - // }; - - // if cls.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) { - // &get_methods() - // } else { - // self.methods.get_or_init(get_methods) - // } } pub fn length_opt(&self, vm: &VirtualMachine) -> Option> { diff --git a/vm/src/stdlib/winapi.rs b/vm/src/stdlib/winapi.rs index fede8d8db..eadfd7efb 100644 --- a/vm/src/stdlib/winapi.rs +++ b/vm/src/stdlib/winapi.rs @@ -4,7 +4,7 @@ pub(crate) use _winapi::make_module; #[pymodule] mod _winapi { use crate::{ - builtins::{PyListRef, PyStrRef}, + builtins::PyStrRef, function::{ArgMapping, IntoPyException, OptionalArg}, stdlib::os::errno_err, PyObjectRef, PyResult, PySequence, TryFromObject, VirtualMachine,