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