diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2024-03-12 21:36:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-12 21:36:21 (GMT) |
commit | a2548077614f81f25a2c3465dabb7a0a3885c40c (patch) | |
tree | 07e48c359d022f298751bfd801b94c529226b20c /Lib/test/support/import_helper.py | |
parent | bb66600558cb8d5dd9a56f562bd9531eb1e1685f (diff) | |
download | cpython-a2548077614f81f25a2c3465dabb7a0a3885c40c.zip cpython-a2548077614f81f25a2c3465dabb7a0a3885c40c.tar.gz cpython-a2548077614f81f25a2c3465dabb7a0a3885c40c.tar.bz2 |
gh-116307: Proper fix for 'mod' leaking across importlib tests (#116680)
gh-116307: Create a new import helper 'isolated modules' and use that instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
Diffstat (limited to 'Lib/test/support/import_helper.py')
-rw-r--r-- | Lib/test/support/import_helper.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index 3d804f2..29c6f53 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -268,6 +268,18 @@ def modules_cleanup(oldmodules): sys.modules.update(oldmodules) +@contextlib.contextmanager +def isolated_modules(): + """ + Save modules on entry and cleanup on exit. + """ + (saved,) = modules_setup() + try: + yield + finally: + modules_cleanup(saved) + + def mock_register_at_fork(func): # bpo-30599: Mock os.register_at_fork() when importing the random module, # since this function doesn't allow to unregister callbacks and would leak |