Skip to content

Commit 1eb67b9

Browse files
committed
fix PyMappingProxy to get only mappiung type
According to [`docs`](https://docs.python.org/3/library/types.html?highlight=mappingproxy#types.MappingProxyType) and [`cpython implementation`](https://github.com/python/cpython/blob/3.9/Objects/descrobject.c#L1190), mapping proxy type must created only with mapping type. As this PR implements mapping protocol, I used it to fix it. Signed-off-by: snowapril <sinjihng@gmail.com>
1 parent b9145dd commit 1eb67b9

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

vm/src/builtins/mappingproxy.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::iterator;
66
use crate::slots::{AsMapping, Iterable, SlotConstructor};
77
use crate::vm::VirtualMachine;
88
use crate::{
9-
IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
10-
TryFromObject, TypeProtocol,
9+
IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult,
10+
PyValue, TryFromObject, TypeProtocol,
1111
};
1212

1313
#[pyclass(module = false, name = "mappingproxy")]
@@ -40,10 +40,20 @@ impl SlotConstructor for PyMappingProxy {
4040
type Args = PyObjectRef;
4141

4242
fn py_new(cls: PyTypeRef, mapping: Self::Args, vm: &VirtualMachine) -> PyResult {
43-
Self {
44-
mapping: MappingProxyInner::Dict(mapping),
43+
if !PyMapping::check(cls, vm)
44+
|| cls.is(&vm.ctx.types.list_type)
45+
|| cls.is(&vm.ctx.types.tuple_type)
46+
{
47+
Err(vm.new_type_error(format!(
48+
"mappingproxy() argument must be a mapping, not {}",
49+
cls
50+
)))
51+
} else {
52+
Self {
53+
mapping: MappingProxyInner::Dict(mapping),
54+
}
55+
.into_pyresult_with_type(vm, cls)
4556
}
46-
.into_pyresult_with_type(vm, cls)
4757
}
4858
}
4959

0 commit comments

Comments
 (0)