Skip to content
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

Proper fix for 'mod' leaking across importlib tests #61

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/support/import_helper.py
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/resources/test_files.py
Expand Up @@ -70,7 +70,7 @@ def setUp(self):
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):
Expand Down
@@ -0,0 +1,3 @@
Added import helper ``isolated_modules`` as ``CleanImport`` does not remove
modules imported during the context. Use it in importlib.resources tests to
avoid leaving ``mod`` around to impede importlib.metadata tests.