--- a/Lib/importlib/test/util.py +++ b/Lib/importlib/test/util.py @@ -24,6 +24,34 @@ "requires a case-insensitive filesystem")(test) +def set_bootstrap(importlib, bootstrap): + """Set importlib to use the passed bootstrap module.""" + importlib._w_long = bootstrap._w_long + importlib._r_long = bootstrap._r_long + importlib._bootstrap = bootstrap + importlib.__import__ = bootstrap.__import__ + sys.modules['importlib._bootstrap'] = bootstrap + + +@contextmanager +def bootstrap_context(importlib, bootstrap): + """Temporarily use an alternate bootstrap module.""" + old_bootstrap = importlib._bootstrap + try: + set_bootstrap(importlib, bootstrap) + yield + finally: + set_bootstrap(importlib, old_bootstrap) + + +def pure_bootstrap_context(importlib): + """Temporarily use importlib._bootstrap.""" + # importlib._bootstrap will normally be the same as _frozen_importlib + if not hasattr(importlib._pure_bootstrap, 'sys'): + importlib._pure_bootstrap._setup(sys, imp) + return bootstrap_context(importlib, importlib._pure_bootstrap) + + @contextmanager def uncache(*names): """Uncache a module from sys.modules.