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

Method py_new

crates/vm/src/builtins/code.rs:592–743  ·  view source on GitHub ↗
(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

590 type Args = PyCodeNewArgs;
591
592 fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
593 // Convert names tuple to vector of interned strings
594 let names: Box<[&'static PyStrInterned]> = args
595 .names
596 .iter()
597 .map(|obj| {
598 let s = obj
599 .downcast_ref::<super::pystr::PyStr>()
600 .ok_or_else(|| vm.new_type_error("names must be tuple of strings"))?;
601 Ok(vm.ctx.intern_str(s.as_wtf8()))
602 })
603 .collect::<PyResult<Vec<_>>>()?
604 .into_boxed_slice();
605
606 let varnames: Box<[&'static PyStrInterned]> = args
607 .varnames
608 .iter()
609 .map(|obj| {
610 let s = obj
611 .downcast_ref::<super::pystr::PyStr>()
612 .ok_or_else(|| vm.new_type_error("varnames must be tuple of strings"))?;
613 Ok(vm.ctx.intern_str(s.as_wtf8()))
614 })
615 .collect::<PyResult<Vec<_>>>()?
616 .into_boxed_slice();
617
618 let cellvars: Box<[&'static PyStrInterned]> = args
619 .cellvars
620 .iter()
621 .map(|obj| {
622 let s = obj
623 .downcast_ref::<super::pystr::PyStr>()
624 .ok_or_else(|| vm.new_type_error("cellvars must be tuple of strings"))?;
625 Ok(vm.ctx.intern_str(s.as_wtf8()))
626 })
627 .collect::<PyResult<Vec<_>>>()?
628 .into_boxed_slice();
629
630 let freevars: Box<[&'static PyStrInterned]> = args
631 .freevars
632 .iter()
633 .map(|obj| {
634 let s = obj
635 .downcast_ref::<super::pystr::PyStr>()
636 .ok_or_else(|| vm.new_type_error("freevars must be tuple of strings"))?;
637 Ok(vm.ctx.intern_str(s.as_wtf8()))
638 })
639 .collect::<PyResult<Vec<_>>>()?
640 .into_boxed_slice();
641
642 // Check nlocals matches varnames length
643 if args.nlocals as usize != varnames.len() {
644 return Err(vm.new_value_error(format!(
645 "nlocals ({}) != len(varnames) ({})",
646 args.nlocals,
647 varnames.len()
648 )));
649 }

Callers

nothing calls this directly

Calls 15

newFunction · 0.85
ok_or_elseMethod · 0.80
intern_strMethod · 0.80
collectMethod · 0.80
to_vecMethod · 0.80
LiteralClass · 0.70
ErrClass · 0.50
mapMethod · 0.45
iterMethod · 0.45
as_wtf8Method · 0.45
lenMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected