Skip to content

Commit 4d779bb

Browse files
committed
isinstance/issubclass - avoid expensive construction of full mro.
1 parent 7894627 commit 4d779bb

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

vm/src/obj/objtype.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ fn _mro(cls: PyObjectRef) -> Option<Vec<PyObjectRef>> {
8181
/// Determines if `obj` actually an instance of `cls`, this doesn't call __instancecheck__, so only
8282
/// use this if `cls` is known to have not overridden the base __instancecheck__ magic method.
8383
pub fn isinstance(obj: &PyObjectRef, cls: &PyObjectRef) -> bool {
84-
let mro = _mro(obj.typ()).unwrap();
85-
mro.into_iter().any(|c| c.is(&cls))
84+
issubclass(&obj.typ(), &cls)
8685
}
8786

8887
fn type_instance_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -98,8 +97,8 @@ fn type_instance_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
9897
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
9998
/// method.
10099
pub fn issubclass(subclass: &PyObjectRef, cls: &PyObjectRef) -> bool {
101-
let mro = _mro(subclass.clone()).unwrap();
102-
mro.into_iter().any(|c| c.is(&cls))
100+
let ref mro = subclass.payload::<PyClass>().unwrap().mro;
101+
subclass.is(&cls) || mro.iter().any(|c| c.is(&cls))
103102
}
104103

105104
fn type_subclass_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)