diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-03-12 23:33:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-12 23:33:23 (GMT) |
commit | f9d1ec8e803012a521f0673da8c5cba1e9862bf5 (patch) | |
tree | 1a060e879f57f17eadcbe6b4457ca351519e8b21 /Lib | |
parent | 65c6620a3aa677ed1315f6ebec134aedf65664c2 (diff) | |
download | cpython-f9d1ec8e803012a521f0673da8c5cba1e9862bf5.zip cpython-f9d1ec8e803012a521f0673da8c5cba1e9862bf5.tar.gz cpython-f9d1ec8e803012a521f0673da8c5cba1e9862bf5.tar.bz2 |
[3.12] gh-116307: Proper fix for 'mod' leaking across importlib tests (GH-116680) (#116684)
gh-116307: Proper fix for 'mod' leaking across importlib tests (GH-116680)
(cherry picked from commit a2548077614f81f25a2c3465dabb7a0a3885c40c)
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.
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/support/import_helper.py | 12 | ||||
-rw-r--r-- | Lib/test/test_importlib/resources/test_files.py | 2 |
2 files changed, 13 insertions, 1 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 diff --git a/Lib/test/test_importlib/resources/test_files.py b/Lib/test/test_importlib/resources/test_files.py index 1450cfb..26c8b04 100644 --- a/Lib/test/test_importlib/resources/test_files.py +++ b/Lib/test/test_importlib/resources/test_files.py @@ -70,7 +70,7 @@ class SiteDir: self.addCleanup(self.fixtures.close) self.site_dir = self.fixtures.enter_context(os_helper.temp_dir()) self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir)) - self.fixtures.enter_context(import_helper.CleanImport()) + self.fixtures.enter_context(import_helper.isolated_modules()) class ModulesFilesTests(SiteDir, unittest.TestCase): |