Skip to content

Commit 48fba05

Browse files
committed
Change .__mro__ to read-only attribute so type.__mro__ works.
1 parent 5742e32 commit 48fba05

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

tests/snippets/mro.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ class C(A, B):
1818
pass
1919

2020
assert (C, A, B, X, Y, object) == C.__mro__
21+
22+
assert type.__mro__ == (type, object)

vm/src/obj/objtype.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::vm::VirtualMachine;
99

1010
use super::objdict;
1111
use super::objlist::PyList;
12+
use super::objproperty::PropertyBuilder;
1213
use super::objstr::{self, PyStringRef};
1314
use super::objtuple::PyTuple;
1415

@@ -64,6 +65,10 @@ impl PyClassRef {
6465
PyTuple::from(_mro(&self))
6566
}
6667

68+
fn set_mro(self, _value: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
69+
Err(vm.new_attribute_error("read-only attribute".to_string()))
70+
}
71+
6772
fn dir(self, vm: &mut VirtualMachine) -> PyList {
6873
let attributes = get_attributes(self);
6974
let attributes: Vec<PyObjectRef> = attributes
@@ -115,7 +120,11 @@ pub fn init(ctx: &PyContext) {
115120
extend_class!(&ctx, &ctx.type_type, {
116121
"__call__" => ctx.new_rustfunc(type_call),
117122
"__new__" => ctx.new_rustfunc(type_new),
118-
"__mro__" => ctx.new_property(PyClassRef::mro),
123+
"__mro__" =>
124+
PropertyBuilder::new(ctx)
125+
.add_getter(PyClassRef::mro)
126+
.add_setter(PyClassRef::set_mro)
127+
.create(),
119128
"__repr__" => ctx.new_rustfunc(PyClassRef::repr),
120129
"__prepare__" => ctx.new_rustfunc(PyClassRef::prepare),
121130
"__getattribute__" => ctx.new_rustfunc(type_getattribute),

0 commit comments

Comments
 (0)