MCPcopy Index your code
hub / github.com/python/cpython / temporary_main_spec

Function temporary_main_spec

Lib/test/test_pyclbr.py:28–46  ·  view source on GitHub ↗

A context manager that temporarily sets the `__spec__` attribute of the `__main__` module if it's missing.

()

Source from the content-addressed store, hash-verified

26
27@contextmanager
28def 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
49class PyclbrTest(TestCase):

Callers 2

test_easyMethod · 0.85
test_othersMethod · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…