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

Method slot_init

crates/vm/src/builtins/object.rs:126–158  ·  view source on GitHub ↗

object_init: excess_args validation

(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

124
125 // object_init: excess_args validation
126 fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
127 if args.is_empty() {
128 return Ok(());
129 }
130
131 let typ = zelf.class();
132 let object_type = &vm.ctx.types.object_type;
133
134 let typ_init = typ.slots.init.load().map(|f| f as usize);
135 let object_init = object_type.slots.init.load().map(|f| f as usize);
136
137 // if (type->tp_init != object_init) → first error
138 if typ_init != object_init {
139 return Err(vm.new_type_error(
140 "object.__init__() takes exactly one argument (the instance to initialize)",
141 ));
142 }
143
144 // if (type->tp_new == object_new) → second error
145 if let (Some(typ_new), Some(object_new)) = (
146 typ.get_attr(identifier!(vm, __new__)),
147 object_type.get_attr(identifier!(vm, __new__)),
148 ) && typ_new.is(&object_new)
149 {
150 return Err(vm.new_type_error(format!(
151 "{}.__init__() takes exactly one argument (the instance to initialize)",
152 typ.name()
153 )));
154 }
155
156 // Both conditions false → OK (e.g., tuple, dict with custom __new__)
157 Ok(())
158 }
159
160 fn init(_zelf: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
161 unreachable!("slot_init is defined")

Callers

nothing calls this directly

Calls 7

isMethod · 0.80
ErrClass · 0.50
is_emptyMethod · 0.45
classMethod · 0.45
mapMethod · 0.45
loadMethod · 0.45
get_attrMethod · 0.45

Tested by

no test coverage detected