classmethod copy attrs (#5831)

This commit is contained in:
Jeong, YunWon authored and GitHub committed 2025-06-24 17:23:37 +09:00
1 parent 9c2a4695c1
commit a8c9703cbc
2 files changed
+25 -12

No files matched your search

-2
View File
@@ -1558,8 +1558,6 @@ order (MRO) for bases """
else:
self.fail("finding the most derived metaclass should have failed")
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_classmethods(self):
# Testing class methods...
class C(object):
+25 -10
View File
@@ -67,19 +67,34 @@ impl Constructor for PyClassMethod {
type Args = PyObjectRef;
fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult {
let doc = callable.get_attr("__doc__", vm);
// Create a dictionary to hold copied attributes
let dict = vm.ctx.new_dict();
let result = PyClassMethod {
// Copy attributes from the callable to the dict
// This is similar to functools.wraps in CPython
if let Ok(doc) = callable.get_attr("__doc__", vm) {
dict.set_item(identifier!(vm.ctx, __doc__), doc, vm)?;
}
if let Ok(name) = callable.get_attr("__name__", vm) {
dict.set_item(identifier!(vm.ctx, __name__), name, vm)?;
}
if let Ok(qualname) = callable.get_attr("__qualname__", vm) {
dict.set_item(identifier!(vm.ctx, __qualname__), qualname, vm)?;
}
if let Ok(module) = callable.get_attr("__module__", vm) {
dict.set_item(identifier!(vm.ctx, __module__), module, vm)?;
}
if let Ok(annotations) = callable.get_attr("__annotations__", vm) {
dict.set_item(identifier!(vm.ctx, __annotations__), annotations, vm)?;
}
// Create PyClassMethod instance with the pre-populated dict
let classmethod = PyClassMethod {
callable: PyMutex::new(callable),
}
.into_ref_with_type(vm, cls)?;
let obj = PyObjectRef::from(result);
};
if let Ok(doc) = doc {
obj.set_attr("__doc__", doc, vm)?;
}
Ok(obj)
let result = PyRef::new_ref(classmethod, cls, Some(dict));
Ok(PyObjectRef::from(result))
}
}