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

Method make_scope

crates/vm/src/stdlib/builtins.rs:475–533  ·  view source on GitHub ↗
(
            self,
            vm: &VirtualMachine,
            func_name: &'static str,
        )

Source from the content-addressed store, hash-verified

473
474 impl ScopeArgs {
475 fn make_scope(
476 self,
477 vm: &VirtualMachine,
478 func_name: &'static str,
479 ) -> PyResult<crate::scope::Scope> {
480 fn validate_globals_dict(
481 globals: &PyObject,
482 vm: &VirtualMachine,
483 func_name: &'static str,
484 ) -> PyResult<()> {
485 if !globals.fast_isinstance(vm.ctx.types.dict_type) {
486 return Err(match func_name {
487 "eval" => {
488 let is_mapping = globals.mapping_unchecked().check();
489 vm.new_type_error(if is_mapping {
490 "globals must be a real dict; try eval(expr, {}, mapping)"
491 .to_owned()
492 } else {
493 "globals must be a dict".to_owned()
494 })
495 }
496 "exec" => vm.new_type_error(format!(
497 "exec() globals must be a dict, not {}",
498 globals.class().name()
499 )),
500 _ => vm.new_type_error("globals must be a dict"),
501 });
502 }
503 Ok(())
504 }
505
506 let (globals, locals) = match self.globals {
507 Some(globals) => {
508 validate_globals_dict(&globals, vm, func_name)?;
509
510 let globals = PyDictRef::try_from_object(vm, globals)?;
511 if !globals.contains_key(identifier!(vm, __builtins__), vm) {
512 let builtins_dict = vm.builtins.dict().into();
513 globals.set_item(identifier!(vm, __builtins__), builtins_dict, vm)?;
514 }
515 (
516 globals.clone(),
517 self.locals
518 .unwrap_or_else(|| ArgMapping::from_dict_exact(globals.clone())),
519 )
520 }
521 None => (
522 vm.current_globals(),
523 if let Some(locals) = self.locals {
524 locals
525 } else {
526 vm.current_locals()?
527 },
528 ),
529 };
530
531 let scope = crate::scope::Scope::with_builtins(Some(locals), globals, vm);
532 Ok(scope)

Callers 2

evalFunction · 0.80
execFunction · 0.80

Calls 7

contains_keyMethod · 0.80
current_globalsMethod · 0.80
current_localsMethod · 0.80
SomeClass · 0.50
dictMethod · 0.45
set_itemMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected