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)
| 170 | # creation when run_module() no longer met the needs of |
| 171 | # mainmodule.c, but couldn't be changed because it was public) |
| 172 | def _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 | |
| 199 | def run_module(mod_name, init_globals=None, |
| 200 | run_name=None, alter_sys=False): |
no test coverage detected
searching dependent graphs…