| 66 | type Args = PyObjectRef; |
| 67 | |
| 68 | fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 69 | let callable: Self::Args = args.bind(vm)?; |
| 70 | // Create a dictionary to hold copied attributes |
| 71 | let dict = vm.ctx.new_dict(); |
| 72 | |
| 73 | // Copy attributes from the callable to the dict |
| 74 | // This is similar to functools.wraps in CPython |
| 75 | if let Ok(doc) = callable.get_attr("__doc__", vm) { |
| 76 | dict.set_item(identifier!(vm.ctx, __doc__), doc, vm)?; |
| 77 | } |
| 78 | if let Ok(name) = callable.get_attr("__name__", vm) { |
| 79 | dict.set_item(identifier!(vm.ctx, __name__), name, vm)?; |
| 80 | } |
| 81 | if let Ok(qualname) = callable.get_attr("__qualname__", vm) { |
| 82 | dict.set_item(identifier!(vm.ctx, __qualname__), qualname, vm)?; |
| 83 | } |
| 84 | if let Ok(module) = callable.get_attr("__module__", vm) { |
| 85 | dict.set_item(identifier!(vm.ctx, __module__), module, vm)?; |
| 86 | } |
| 87 | if let Ok(annotations) = callable.get_attr("__annotations__", vm) { |
| 88 | dict.set_item(identifier!(vm.ctx, __annotations__), annotations, vm)?; |
| 89 | } |
| 90 | |
| 91 | // Create PyClassMethod instance with the pre-populated dict |
| 92 | let classmethod = Self { |
| 93 | callable: PyMutex::new(callable), |
| 94 | }; |
| 95 | |
| 96 | let result = PyRef::new_ref(classmethod, cls, Some(dict)); |
| 97 | Ok(PyObjectRef::from(result)) |
| 98 | } |
| 99 | |
| 100 | fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> { |
| 101 | unimplemented!("use slot_new") |