Skip to content

Commit

Permalink
[3.11] gh-116307: Proper fix for 'mod' leaking across importlib tests… (
Browse files Browse the repository at this point in the history
#116694)

[3.11] gh-116307: Proper fix for 'mod' leaking across importlib tests (GH-116680)
(cherry picked from commit a254807)


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.
  • Loading branch information
jaraco committed Mar 13, 2024
1 parent a01621a commit 21a259e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Lib/test/support/import_helper.py
Expand Up @@ -248,6 +248,26 @@ 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
# memory.
from unittest import mock
return mock.patch('os.register_at_fork', create=True)(func)


@contextlib.contextmanager
def ready_to_import(name=None, source=""):
from test.support import script_helper
Expand Down
@@ -0,0 +1,2 @@
Added import helper ``isolated_modules`` as ``CleanImport`` does not remove
modules imported during the context.

0 comments on commit 21a259e

Please sign in to comment.