A context manager that temporarily sets the `__spec__` attribute of the `__main__` module if it's missing.
()
| 26 | |
| 27 | @contextmanager |
| 28 | def temporary_main_spec(): |
| 29 | """ |
| 30 | A context manager that temporarily sets the `__spec__` attribute |
| 31 | of the `__main__` module if it's missing. |
| 32 | """ |
| 33 | main_mod = sys.modules.get("__main__") |
| 34 | if main_mod is None: |
| 35 | yield # Do nothing if __main__ is not present |
| 36 | return |
| 37 | |
| 38 | original_spec = getattr(main_mod, "__spec__", None) |
| 39 | if original_spec is None: |
| 40 | main_mod.__spec__ = importlib.machinery.ModuleSpec( |
| 41 | name="__main__", loader=None, origin="built-in" |
| 42 | ) |
| 43 | try: |
| 44 | yield |
| 45 | finally: |
| 46 | main_mod.__spec__ = original_spec |
| 47 | |
| 48 | |
| 49 | class PyclbrTest(TestCase): |
no test coverage detected
searching dependent graphs…