diff --git a/vm/src/builtins/pytype.rs b/vm/src/builtins/pytype.rs index a784f14e4..3ea2561d7 100644 --- a/vm/src/builtins/pytype.rs +++ b/vm/src/builtins/pytype.rs @@ -12,8 +12,8 @@ use crate::{ pyclass::{PyClassImpl, StaticType}, pyobject::PyLease, types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr}, - IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, - TypeProtocol, VirtualMachine, + IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol, + VirtualMachine, }; use itertools::Itertools; use std::{ @@ -209,8 +209,11 @@ impl PyType { } impl PyTypeRef { + /// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__, + /// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic + /// method. pub fn issubclass(&self, cls: &impl Borrow) -> bool { - self._issubclass(cls) + self.as_object().is(cls.borrow()) || self.mro.iter().any(|c| c.is(cls.borrow())) } pub fn iter_mro(&self) -> impl Iterator + DoubleEndedIterator { @@ -788,36 +791,6 @@ pub(crate) fn init(ctx: &PyContext) { PyType::extend_class(ctx, &ctx.types.type_type); } -impl PyLease<'_, PyType> { - pub fn issubclass(&self, cls: &impl Borrow) -> bool { - self._issubclass(cls) - } -} - -pub trait DerefToPyType: Borrow { - fn deref_to_type(&self) -> &PyType; - - /// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__, - /// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic - /// method. - fn _issubclass(&self, cls: &impl Borrow) -> bool { - self.borrow().is(cls.borrow()) - || self.deref_to_type().mro.iter().any(|c| c.is(cls.borrow())) - } -} - -impl DerefToPyType for PyTypeRef { - fn deref_to_type(&self) -> &PyType { - self.deref() - } -} - -impl<'a> DerefToPyType for PyLease<'a, PyType> { - fn deref_to_type(&self) -> &PyType { - self.deref() - } -} - pub(crate) fn call_slot_new( typ: PyTypeRef, subtype: PyTypeRef, diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index c850d20c5..df5f3cadb 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1,7 +1,7 @@ pub use crate::_pyobjectrc::{ PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef, }; -use crate::common::{lock::PyRwLockReadGuard, rc::PyRc}; +use crate::common::lock::PyRwLockReadGuard; use crate::{ builtins::{ builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef}, @@ -445,7 +445,7 @@ impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> { impl<'a, T: PyObjectPayload + PyValue> Borrow for PyLease<'a, T> { fn borrow(&self) -> &PyObject { - self.inner.as_object() + self.inner.as_ref() } } diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index c45034b1d..a3f2fc25b 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -466,11 +466,11 @@ impl ToOwned for PyObject { pub trait PyObjectWrap where - Self: Borrow + AsRef, + Self: Borrow, { #[inline(always)] fn as_object(&self) -> &PyObject { - self.as_ref() + self.borrow() } fn into_object(self) -> PyObjectRef; @@ -986,7 +986,17 @@ where T: PyObjectPayload, { fn borrow(&self) -> &PyObject { - self.as_object() + (**self).as_object() + } +} + +impl AsRef for PyRef +where + T: PyObjectPayload, +{ + #[inline(always)] + fn as_ref(&self) -> &PyObject { + self.borrow() } } @@ -1001,16 +1011,6 @@ where } } -impl AsRef for PyRef -where - T: PyObjectPayload, -{ - #[inline(always)] - fn as_ref(&self) -> &PyObject { - (**self).as_object() - } -} - impl Borrow> for PyRef where T: PyObjectPayload,