Skip to content

Commit f0e285d

Browse files
Merge pull request RustPython#665 from RustPython/type_mro
Type mro
2 parents 83788b9 + 48fba05 commit f0e285d

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
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/objproperty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, T> PropertyBuilder<'a, T> {
119119
}
120120
}
121121

122-
pub fn add_setter<I, F: IntoPyNativeFunc<(I, T), PyResult>>(self, func: F) -> Self {
122+
pub fn add_setter<I, V, F: IntoPyNativeFunc<(I, V), PyResult>>(self, func: F) -> Self {
123123
let func = self.ctx.new_rustfunc(func);
124124
Self {
125125
ctx: self.ctx,

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

@@ -78,6 +79,10 @@ impl PyClassRef {
7879
PyTuple::from(elements)
7980
}
8081

82+
fn set_mro(self, _value: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
83+
Err(vm.new_attribute_error("read-only attribute".to_string()))
84+
}
85+
8186
fn dir(self, vm: &mut VirtualMachine) -> PyList {
8287
let attributes = get_attributes(self);
8388
let attributes: Vec<PyObjectRef> = attributes
@@ -116,7 +121,11 @@ pub fn init(ctx: &PyContext) {
116121
extend_class!(&ctx, &ctx.type_type, {
117122
"__call__" => ctx.new_rustfunc(type_call),
118123
"__new__" => ctx.new_rustfunc(type_new),
119-
"__mro__" => ctx.new_property(PyClassRef::mro),
124+
"__mro__" =>
125+
PropertyBuilder::new(ctx)
126+
.add_getter(PyClassRef::mro)
127+
.add_setter(PyClassRef::set_mro)
128+
.create(),
120129
"__repr__" => ctx.new_rustfunc(PyClassRef::repr),
121130
"__prepare__" => ctx.new_rustfunc(PyClassRef::prepare),
122131
"__getattribute__" => ctx.new_rustfunc(type_getattribute),

0 commit comments

Comments
 (0)