From 6ca4685fee775f1e84a5decc1d64bc94c33e480a Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 29 Jul 2024 00:15:04 +0900 Subject: [PATCH] __objclass__ --- .cspell.json | 1 + Lib/test/test_descr.py | 2 -- vm/src/builtins/descriptor.rs | 8 +++++++- vm/src/builtins/getset.rs | 7 ++++++- vm/src/function/method.rs | 7 ++----- vm/src/vm/context.rs | 1 + 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.cspell.json b/.cspell.json index ad90153f5..e2723d6ce 100644 --- a/.cspell.json +++ b/.cspell.json @@ -52,6 +52,7 @@ "metas", "modpow", "nanos", + "objclass", "peekable", "powc", "powf", diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 8389ce67c..eae8b42fc 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4635,8 +4635,6 @@ order (MRO) for bases """ # hash([].append) should not be based on hash([]) hash(l.append) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_special_unbound_method_types(self): # Testing objects of ... self.assertTrue(list.__add__ == list.__add__) diff --git a/vm/src/builtins/descriptor.rs b/vm/src/builtins/descriptor.rs index 6f0705e8e..16340c3a6 100644 --- a/vm/src/builtins/descriptor.rs +++ b/vm/src/builtins/descriptor.rs @@ -1,6 +1,6 @@ use super::{PyStr, PyStrInterned, PyType}; use crate::{ - builtins::{builtin_func::PyNativeMethod, type_}, + builtins::{builtin_func::PyNativeMethod, type_, PyTypeRef}, class::PyClassImpl, function::{FuncArgs, PyMethodDef, PyMethodFlags, PySetterValue}, types::{Callable, GetDescriptor, Representable, Unconstructible}, @@ -27,6 +27,7 @@ pub struct PyMethodDescriptor { pub common: PyDescriptor, pub method: &'static PyMethodDef, // vectorcall: vectorcallfunc, + pub objclass: &'static Py, // TODO: move to tp_members } impl PyMethodDescriptor { @@ -38,6 +39,7 @@ impl PyMethodDescriptor { qualname: PyRwLock::new(None), }, method, + objclass: typ, } } } @@ -126,6 +128,10 @@ impl PyMethodDescriptor { .map(|signature| signature.to_string()) }) } + #[pygetset(magic)] + fn objclass(&self) -> PyTypeRef { + self.objclass.to_owned() + } #[pymethod(magic)] fn reduce( &self, diff --git a/vm/src/builtins/getset.rs b/vm/src/builtins/getset.rs index 2fa1e4148..603ec021e 100644 --- a/vm/src/builtins/getset.rs +++ b/vm/src/builtins/getset.rs @@ -1,7 +1,7 @@ /*! Python `attribute` descriptor class. (PyGetSet) */ -use super::PyType; +use super::{PyType, PyTypeRef}; use crate::{ class::PyClassImpl, function::{IntoPyGetterFunc, IntoPySetterFunc, PyGetterFunc, PySetterFunc, PySetterValue}, @@ -139,6 +139,11 @@ impl PyGetSet { fn qualname(&self) -> String { format!("{}.{}", self.class.slot_name(), self.name.clone()) } + + #[pygetset(magic)] + fn objclass(&self) -> PyTypeRef { + self.class.to_owned() + } } impl Unconstructible for PyGetSet {} diff --git a/vm/src/function/method.rs b/vm/src/function/method.rs index 513cb9383..c8652ffb5 100644 --- a/vm/src/function/method.rs +++ b/vm/src/function/method.rs @@ -170,11 +170,8 @@ impl PyMethodDef { class: &'static Py, ) -> PyRef { debug_assert!(self.flags.contains(PyMethodFlags::METHOD)); - PyRef::new_ref( - self.to_method(class, ctx), - ctx.types.method_descriptor_type.to_owned(), - None, - ) + let method = self.to_method(class, ctx); + PyRef::new_ref(method, ctx.types.method_descriptor_type.to_owned(), None) } pub fn build_bound_method( &'static self, diff --git a/vm/src/vm/context.rs b/vm/src/vm/context.rs index 217d4eecf..f67035d0f 100644 --- a/vm/src/vm/context.rs +++ b/vm/src/vm/context.rs @@ -174,6 +174,7 @@ declare_const_name! { __neg__, __new__, __next__, + __objclass__, __or__, __orig_bases__, __orig_class__,