fix ass_subscript check

This commit is contained in:
Kangzhi Shi
2021-12-01 20:40:04 +02:00
parent 6a996b04c8
commit ae538943c0
3 changed files with 9 additions and 7 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::from(mapping.as_ref()).has_protocol(vm)
if !PyMapping::from(mapping.as_ref()).check(vm)
|| mapping.payload_if_subclass::<PyList>(vm).is_some()
|| mapping.payload_if_subclass::<PyTuple>(vm).is_some()
{

View File

@@ -50,7 +50,7 @@ impl<'a> PyMapping<'a> {
pub fn try_protocol(obj: &'a PyObject, vm: &VirtualMachine) -> PyResult<Self> {
let zelf = Self::from(obj);
if zelf.has_protocol(vm) {
if zelf.check(vm) {
Ok(zelf)
} else {
Err(vm.new_type_error(format!("{} is not a mapping object", zelf.obj.class())))
@@ -60,7 +60,7 @@ impl<'a> PyMapping<'a> {
impl PyMapping<'_> {
// PyMapping::Check
pub fn has_protocol(&self, vm: &VirtualMachine) -> bool {
pub fn check(&self, vm: &VirtualMachine) -> bool {
self.methods(vm).subscript.is_some()
}

View File

@@ -464,8 +464,9 @@ impl PyObject {
let needle = needle.into_pyobject(vm);
if let Ok(mapping) = PyMapping::try_protocol(self, vm) {
mapping.ass_subscript(&needle, Some(value), vm)
let mapping = PyMapping::from(self);
if let Some(f) = mapping.methods(vm).ass_subscript {
f(&mapping, &needle, Some(value), vm)
} else {
// TODO: sequence protocol
vm.get_special_method(self.to_owned(), "__setitem__")?
@@ -491,8 +492,9 @@ impl PyObject {
let needle = needle.into_pyobject(vm);
if let Ok(mapping) = PyMapping::try_protocol(self, vm) {
mapping.ass_subscript(&needle, None, vm)
let mapping = PyMapping::from(self);
if let Some(f) = mapping.methods(vm).ass_subscript {
f(&mapping, &needle, None, vm)
} else {
//TODO: sequence protocol
vm.get_special_method(self.to_owned(), "__delitem__")?