Helper to run code in new namespace with sys modified
(code, init_globals=None, mod_name=None, mod_spec=None, pkg_name=None, script_name=None)
| 120 | |
| 121 | |
| 122 | def _run_module_code(code, init_globals=None, mod_name=None, mod_spec=None, pkg_name=None, script_name=None): |
| 123 | """Helper to run code in new namespace with sys modified""" |
| 124 | fname = script_name if mod_spec is None else mod_spec.origin |
| 125 | with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname): |
| 126 | mod_globals = temp_module.module.__dict__ |
| 127 | _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name) |
| 128 | # Copy the globals of the temporary module, as they |
| 129 | # may be cleared when the temporary module goes away |
| 130 | return mod_globals.copy() |
| 131 | |
| 132 | |
| 133 | # Helper to get the full name, spec and code for a module |
no test coverage detected