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

Function setup_context

crates/vm/src/warn.rs:548–611  ·  view source on GitHub ↗

filename, module, and registry are new refs, globals is borrowed Returns `Ok` on success, or `Err` on error (no new refs)

(
    mut stack_level: isize,
    skip_file_prefixes: Option<&PyTupleRef>,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

546/// filename, module, and registry are new refs, globals is borrowed
547/// Returns `Ok` on success, or `Err` on error (no new refs)
548fn setup_context(
549 mut stack_level: isize,
550 skip_file_prefixes: Option<&PyTupleRef>,
551 vm: &VirtualMachine,
552) -> PyResult<(PyStrRef, usize, Option<PyObjectRef>, PyObjectRef)> {
553 let mut f = vm.current_frame();
554
555 // Stack level comparisons to Python code is off by one as there is no
556 // warnings-related stack level to avoid.
557 if stack_level <= 0 || f.as_ref().is_some_and(|frame| frame.is_internal_frame()) {
558 while {
559 stack_level -= 1;
560 stack_level > 0
561 } {
562 match f {
563 Some(tmp) => f = tmp.f_back(vm),
564 None => break,
565 }
566 }
567 } else {
568 while {
569 stack_level -= 1;
570 stack_level > 0
571 } {
572 match f {
573 Some(tmp) => f = next_external_frame_with_skip(&tmp, skip_file_prefixes, vm),
574 None => break,
575 }
576 }
577 }
578
579 let (globals, filename, lineno) = if let Some(f) = f {
580 (f.globals.clone(), f.code.source_path(), f.f_lineno())
581 } else if let Some(frame) = vm.current_frame() {
582 // We have a frame but it wasn't found during stack walking
583 (frame.globals.clone(), vm.ctx.intern_str("<sys>"), 1)
584 } else {
585 // No frames on the stack - use sys.__dict__ (interp->sysdict)
586 let globals = vm
587 .sys_module
588 .as_object()
589 .get_attr(identifier!(vm, __dict__), vm)
590 .and_then(|d| {
591 d.downcast::<crate::builtins::PyDict>()
592 .map_err(|_| vm.new_type_error("sys.__dict__ is not a dictionary"))
593 })?;
594 (globals, vm.ctx.intern_str("<sys>"), 0)
595 };
596
597 let registry = match globals.get_item("__warningregistry__", vm) {
598 Ok(r) => r,
599 Err(_) => {
600 let r = vm.ctx.new_dict();
601 globals.set_item("__warningregistry__", r.clone().into(), vm)?;
602 r.into()
603 }
604 };
605

Callers 1

warn_with_skipFunction · 0.85

Calls 15

current_frameMethod · 0.80
is_internal_frameMethod · 0.80
f_backMethod · 0.80
f_linenoMethod · 0.80
intern_strMethod · 0.80
new_dictMethod · 0.80
new_pyobjMethod · 0.80
SomeClass · 0.50
as_refMethod · 0.45
cloneMethod · 0.45
source_pathMethod · 0.45

Tested by

no test coverage detected