Skip to content

Commit ccd8bea

Browse files
authored
pythongh-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. (#61)
1 parent db45852 commit ccd8bea

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/test/support/import_helper.py

+12
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ def modules_cleanup(oldmodules):
268268
sys.modules.update(oldmodules)
269269

270270

271+
@contextlib.contextmanager
272+
def isolated_modules():
273+
"""
274+
Save modules on entry and cleanup on exit.
275+
"""
276+
(saved,) = modules_setup()
277+
try:
278+
yield
279+
finally:
280+
modules_cleanup(saved)
281+
282+
271283
def mock_register_at_fork(func):
272284
# bpo-30599: Mock os.register_at_fork() when importing the random module,
273285
# since this function doesn't allow to unregister callbacks and would leak

Lib/test/test_importlib/resources/test_files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def setUp(self):
7070
self.addCleanup(self.fixtures.close)
7171
self.site_dir = self.fixtures.enter_context(os_helper.temp_dir())
7272
self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir))
73-
self.fixtures.enter_context(import_helper.CleanImport())
73+
self.fixtures.enter_context(import_helper.isolated_modules())
7474

7575

7676
class ModulesFilesTests(SiteDir, unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added import helper ``isolated_modules`` as ``CleanImport`` does not remove
2+
modules imported during the context. Use it in importlib.resources tests to
3+
avoid leaving ``mod`` around to impede importlib.metadata tests.

0 commit comments

Comments
 (0)