MCPcopy Index your code
hub / github.com/RustPython/RustPython / slot_new

Method slot_new

crates/vm/src/builtins/classmethod.rs:68–98  ·  view source on GitHub ↗
(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

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")

Callers

nothing calls this directly

Calls 6

newFunction · 0.85
new_dictMethod · 0.80
SomeClass · 0.50
bindMethod · 0.45
get_attrMethod · 0.45
set_itemMethod · 0.45

Tested by

no test coverage detected