MCPcopy Index your code
hub / github.com/python/cpython / _run_module_as_main

Function _run_module_as_main

Lib/runpy.py:172–197  ·  view source on GitHub ↗

Runs the designated module in the __main__ namespace Note that the executed module will have full access to the __main__ namespace. If this is not desirable, the run_module() function should be used to run the module code in a fresh namespace. At the very least, these v

(mod_name, alter_argv=True)

Source from the content-addressed store, hash-verified

170# creation when run_module() no longer met the needs of
171# mainmodule.c, but couldn't be changed because it was public)
172def _run_module_as_main(mod_name, alter_argv=True):
173 """Runs the designated module in the __main__ namespace
174
175 Note that the executed module will have full access to the
176 __main__ namespace. If this is not desirable, the run_module()
177 function should be used to run the module code in a fresh namespace.
178
179 At the very least, these variables in __main__ will be overwritten:
180 __name__
181 __file__
182 __loader__
183 __package__
184 """
185 try:
186 if alter_argv or mod_name != "__main__": # i.e. -m switch
187 mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
188 else: # i.e. directory or zipfile execution
189 mod_name, mod_spec, code = _get_main_module_details(_Error)
190 except _Error as exc:
191 msg = "%s: %s" % (sys.executable, exc)
192 sys.exit(msg)
193 main_globals = sys.modules["__main__"].__dict__
194 if alter_argv:
195 sys.argv[0] = mod_spec.origin
196 return _run_code(code, main_globals, None,
197 "__main__", mod_spec)
198
199def run_module(mod_name, init_globals=None,
200 run_name=None, alter_sys=False):

Callers 1

runpy.pyFile · 0.85

Calls 4

_get_module_detailsFunction · 0.85
_get_main_module_detailsFunction · 0.85
_run_codeFunction · 0.85
exitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…