Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-45020: Freeze the modules imported during startup.#28107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a8bd3fc6555ae43a63f92058732898814563df3e23bfd733cca952fcb4ca34cef5f3f74b630b8dd1a498af074894e4baeb344a5ec8710d8077cf8012702554becae8f7f0c8ebd07f63c4787a70a75b8082050793e16771de5b4da78b54908a82b06aead34a158ccaff6d068b11021f53c01b5320c1061dfa2300903b9571b3a7aaf6a896f1211febefbaf15c6243c54da978a246d488f93fc5c0dd36ba545e7509a7d607f75556af37b79f1d93c1e725b60de358ab691fc119837eb1f82e0577182b4f85097fc40a333fa38abf4a010053217d62747File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -109,7 +109,20 @@ def _save_and_block_module(name, orig_modules): | ||
| return saved | ||
| def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): | ||
| @contextlib.contextmanager | ||
| def frozen_modules(enabled=True): | ||
| # FYI: the env var will never show up in os.environ. | ||
| os.putenv('_PYTHONTESTFROZENMODULES', '1' if enabled else '0') | ||
| try: | ||
| yield | ||
| finally: | ||
| os.unsetenv('_PYTHONTESTFROZENMODULES') | ||
| def import_fresh_module(name, fresh=(), blocked=(), *, | ||
| deprecated=False, | ||
| usefrozen=False, | ||
| ): | ||
| """Import and return a module, deliberately bypassing sys.modules. | ||
| This function imports and returns a fresh copy of the named Python module | ||
| @@ -148,7 +161,8 @@ def import_fresh_module(name, fresh=(), blocked=(), deprecated=False): | ||
| for blocked_name in blocked: | ||
| if not _save_and_block_module(blocked_name, orig_modules): | ||
| names_to_remove.append(blocked_name) | ||
| fresh_module = importlib.import_module(name) | ||
| with frozen_modules(usefrozen): | ||
| fresh_module = importlib.import_module(name) | ||
| except ImportError: | ||
| fresh_module = None | ||
| finally: | ||
| @@ -171,7 +185,7 @@ class CleanImport(object): | ||
| importlib.import_module("foo") # new reference | ||
| """ | ||
| def __init__(self, *module_names): | ||
| def __init__(self, *module_names, usefrozen=False): | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the class docstring to mention that it also disabled frozen modules unless usefrozen=True is passed? | ||
| self.original_modules = sys.modules.copy() | ||
| for module_name in module_names: | ||
| if module_name in sys.modules: | ||
| @@ -183,12 +197,15 @@ def __init__(self, *module_names): | ||
| if module.__name__ != module_name: | ||
| del sys.modules[module.__name__] | ||
| del sys.modules[module_name] | ||
| self._frozen_modules = frozen_modules(usefrozen) | ||
| def __enter__(self): | ||
| self._frozen_modules.__enter__() | ||
| return self | ||
| def __exit__(self, *ignore_exc): | ||
| sys.modules.update(self.original_modules) | ||
| self._frozen_modules.__exit__(*ignore_exc) | ||
| class DirsOnSysPath(object): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -474,7 +474,6 @@ def test_dash_m_errors(self): | ||
| br'ModuleNotFoundError'), | ||
| ('builtins.x.y', br'Error while finding module specification.*' | ||
| br'ModuleNotFoundError.*No module named.*not a package'), | ||
| ('os.path', br'loader.*cannot handle'), | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's another failure (two actually) in this test: test_module_in_[sub]package_in_zipfile are both failing. The crucial error seems to be this (from a subprocess): Note that MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I fixed this by adding "zipimport" back as one of the essential frozen modules. | ||
| ('importlib', br'No module named.*' | ||
| br'is a package and cannot be directly executed'), | ||
| ('importlib.nonexistent', br'No module named'), | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What stdout content is being captured?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The frozen test module (hello.py) prints out
Hello world!.