Merge pull request #3313 from DimitrisJim/mapping_check

Check if subscript is present for PyMapping::check.
This commit is contained in:
Jeong YunWon
2021-10-15 19:43:01 +09:00
committed by GitHub
2 changed files with 5 additions and 4 deletions

View File

@@ -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::<PyList>(vm).is_some()
|| mapping.payload_if_subclass::<PyTuple>(vm).is_some()
{

View File

@@ -26,10 +26,11 @@ where
T: Borrow<PyObjectRef>;
impl PyMapping<PyObjectRef> {
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<PyObjectRef> {
impl TryFromObject for PyMapping<PyObjectRef> {
fn try_from_object(vm: &VirtualMachine, mapping: PyObjectRef) -> PyResult<Self> {
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())))