From 9b63c50446a4682384bd7f61a71c142c868f0c69 Mon Sep 17 00:00:00 2001 From: jfh Date: Fri, 15 Oct 2021 09:46:18 +0300 Subject: [PATCH] Check if subscript is present for PyMapping::check. --- vm/src/builtins/mappingproxy.rs | 2 +- vm/src/protocol/mapping.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/vm/src/builtins/mappingproxy.rs b/vm/src/builtins/mappingproxy.rs index 9df9c0b7e..aaf43ae61 100644 --- a/vm/src/builtins/mappingproxy.rs +++ b/vm/src/builtins/mappingproxy.rs @@ -37,7 +37,7 @@ impl Constructor for PyMappingProxy { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, mapping: Self::Args, vm: &VirtualMachine) -> PyResult { - if !PyMapping::check(&mapping) + if !PyMapping::check(&mapping, vm) || mapping.payload_if_subclass::(vm).is_some() || mapping.payload_if_subclass::(vm).is_some() { diff --git a/vm/src/protocol/mapping.rs b/vm/src/protocol/mapping.rs index 6b4a1dcfe..b4416cbc8 100644 --- a/vm/src/protocol/mapping.rs +++ b/vm/src/protocol/mapping.rs @@ -26,10 +26,11 @@ where T: Borrow; impl PyMapping { - pub fn check(obj: &PyObjectRef) -> bool { + pub fn check(obj: &PyObjectRef, vm: &VirtualMachine) -> bool { obj.class() .mro_find_map(|x| x.slots.as_mapping.load()) - .is_some() + .map(|f| f(obj, vm).subscript.is_some()) + .unwrap_or(false) } pub fn methods(&self, vm: &VirtualMachine) -> PyMappingMethods { @@ -119,7 +120,7 @@ impl IntoPyObject for PyMapping { impl TryFromObject for PyMapping { fn try_from_object(vm: &VirtualMachine, mapping: PyObjectRef) -> PyResult { - if Self::check(&mapping) { + if Self::check(&mapping, vm) { Ok(Self::new(mapping)) } else { Err(vm.new_type_error(format!("{} is not a mapping object", mapping.class())))