| 273 | |
| 274 | |
| 275 | def _fixup_main_from_path(main_path): |
| 276 | # If this process was forked, __main__ may already be populated |
| 277 | current_main = sys.modules['__main__'] |
| 278 | |
| 279 | # Unfortunately, the main ipython launch script historically had no |
| 280 | # "if __name__ == '__main__'" guard, so we work around that |
| 281 | # by treating it like a __main__.py file |
| 282 | # See https://github.com/ipython/ipython/issues/4698 |
| 283 | main_name = os.path.splitext(os.path.basename(main_path))[0] |
| 284 | if main_name == 'ipython': |
| 285 | return |
| 286 | |
| 287 | # Otherwise, if __file__ already has the setting we expect, |
| 288 | # there's nothing more to do |
| 289 | if getattr(current_main, '__file__', None) == main_path: |
| 290 | return |
| 291 | |
| 292 | # If the parent process has sent a path through rather than a module |
| 293 | # name we assume it is an executable script that may contain |
| 294 | # non-main code that needs to be executed |
| 295 | old_main_modules.append(current_main) |
| 296 | main_module = types.ModuleType("__mp_main__") |
| 297 | main_content = runpy.run_path(main_path, |
| 298 | run_name="__mp_main__") |
| 299 | main_module.__dict__.update(main_content) |
| 300 | sys.modules['__main__'] = sys.modules['__mp_main__'] = main_module |
| 301 | |
| 302 | |
| 303 | def import_main_path(main_path): |