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 variables in
(mod_name, alter_argv=True)
| 201 | # creation when run_module() no longer met the needs of |
| 202 | # mainmodule.c, but couldn't be changed because it was public) |
| 203 | def _run_module_as_main(mod_name, alter_argv=True): |
| 204 | """Runs the designated module in the __main__ namespace |
| 205 | |
| 206 | Note that the executed module will have full access to the |
| 207 | __main__ namespace. If this is not desirable, the run_module() |
| 208 | function should be used to run the module code in a fresh namespace. |
| 209 | |
| 210 | At the very least, these variables in __main__ will be overwritten: |
| 211 | __name__ |
| 212 | __file__ |
| 213 | __cached__ |
| 214 | __loader__ |
| 215 | __package__ |
| 216 | """ |
| 217 | try: |
| 218 | if alter_argv or mod_name != "__main__": # i.e. -m switch |
| 219 | mod_name, mod_spec, code = _get_module_details(mod_name, _Error) |
| 220 | else: # i.e. directory or zipfile execution |
| 221 | mod_name, mod_spec, code = _get_main_module_details(_Error) |
| 222 | except _Error as exc: |
| 223 | msg = "%s: %s" % (sys.executable, exc) |
| 224 | sys.exit(msg) |
| 225 | main_globals = sys.modules["__main__"].__dict__ |
| 226 | if alter_argv: |
| 227 | sys.argv[0] = mod_spec.origin |
| 228 | return _run_code(code, main_globals, None, "__main__", mod_spec) |
| 229 | |
| 230 | |
| 231 | def run_module(mod_name, init_globals=None, run_name=None, alter_sys=False): |
no test coverage detected