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

Method slot_new

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

Source from the content-addressed store, hash-verified

249 type Args = FuncArgs;
250
251 fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
252 if cls.is(vm.ctx.types.bool_type) {
253 return Err(vm.new_type_error("int.__new__(bool) is not safe, use bool.__new__()"));
254 }
255
256 // Optimization: return exact int as-is (only for exact int type, not subclasses)
257 if cls.is(vm.ctx.types.int_type)
258 && args.args.len() == 1
259 && args.kwargs.is_empty()
260 && args.args[0].class().is(vm.ctx.types.int_type)
261 {
262 return Ok(args.args[0].clone());
263 }
264
265 let options: IntOptions = args.bind(vm)?;
266 let value = if let OptionalArg::Present(val) = options.val_options {
267 if let OptionalArg::Present(base) = options.base {
268 let base = base
269 .try_index(vm)?
270 .as_bigint()
271 .to_u32()
272 .filter(|&v| v == 0 || (2..=36).contains(&v))
273 .ok_or_else(|| vm.new_value_error("int() base must be >= 2 and <= 36, or 0"))?;
274 try_int_radix(&val, base, vm)
275 } else {
276 val.try_int(vm).map(|x| x.as_bigint().clone())
277 }
278 } else if let OptionalArg::Present(_) = options.base {
279 Err(vm.new_type_error("int() missing string argument"))
280 } else {
281 Ok(Zero::zero())
282 }?;
283
284 Self::with_value(cls, value, vm).map(Into::into)
285 }
286
287 fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
288 unimplemented!("use slot_new")

Callers

nothing calls this directly

Calls 15

try_int_radixFunction · 0.85
isMethod · 0.80
ok_or_elseMethod · 0.80
as_bigintMethod · 0.80
try_indexMethod · 0.80
try_intMethod · 0.80
ErrClass · 0.50
lenMethod · 0.45
is_emptyMethod · 0.45
classMethod · 0.45
cloneMethod · 0.45
bindMethod · 0.45

Tested by

no test coverage detected