Real isinstance check without going through __instancecheck__ This is equivalent to CPython's _PyObject_RealIsInstance/object_isinstance
(&self, cls: &Self, vm: &VirtualMachine)
| 582 | /// Real isinstance check without going through __instancecheck__ |
| 583 | /// This is equivalent to CPython's _PyObject_RealIsInstance/object_isinstance |
| 584 | fn object_isinstance(&self, cls: &Self, vm: &VirtualMachine) -> PyResult<bool> { |
| 585 | if let Ok(cls) = cls.try_to_ref::<PyType>(vm) { |
| 586 | // PyType_Check(cls) - cls is a type object |
| 587 | let mut retval = self.class().is_subtype(cls); |
| 588 | if !retval |
| 589 | && let Some(i_cls) = |
| 590 | vm.get_attribute_opt(self.to_owned(), identifier!(vm, __class__))? |
| 591 | && let Ok(i_cls_type) = PyTypeRef::try_from_object(vm, i_cls) |
| 592 | && !i_cls_type.is(self.class()) |
| 593 | { |
| 594 | retval = i_cls_type.is_subtype(cls); |
| 595 | } |
| 596 | Ok(retval) |
| 597 | } else { |
| 598 | // Not a type object, check if it's a valid class |
| 599 | cls.check_class(vm, || { |
| 600 | format!( |
| 601 | "isinstance() arg 2 must be a type, a tuple of types, or a union, not {}", |
| 602 | cls.class() |
| 603 | ) |
| 604 | })?; |
| 605 | |
| 606 | if let Some(i_cls) = |
| 607 | vm.get_attribute_opt(self.to_owned(), identifier!(vm, __class__))? |
| 608 | { |
| 609 | i_cls.abstract_issubclass(cls, vm) |
| 610 | } else { |
| 611 | Ok(false) |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /// Determines if `self` is an instance of `cls`, either directly, indirectly or virtually via |
| 617 | /// the __instancecheck__ magic method. |
no test coverage detected