]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-116307: Proper fix for 'mod' leaking across importlib tests… (#116694)
authorJason R. Coombs <jaraco@jaraco.com>
Wed, 13 Mar 2024 00:35:00 +0000 (20:35 -0400)
committerGitHub <noreply@github.com>
Wed, 13 Mar 2024 00:35:00 +0000 (00:35 +0000)
[3.11] 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.

Lib/test/support/import_helper.py
Misc/NEWS.d/next/Tests/2024-03-06-11-00-36.gh-issue-116307.Uij0t_.rst [new file with mode: 0644]

index a803d9f1b4039a3aa4e8fd0553e49fbc0ea824ee..8519194381a6ad75de371791a0e086095e9781ae 100644 (file)
@@ -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
diff --git a/Misc/NEWS.d/next/Tests/2024-03-06-11-00-36.gh-issue-116307.Uij0t_.rst b/Misc/NEWS.d/next/Tests/2024-03-06-11-00-36.gh-issue-116307.Uij0t_.rst
new file mode 100644 (file)
index 0000000..93b62d1
--- /dev/null
@@ -0,0 +1,2 @@
+Added import helper ``isolated_modules`` as ``CleanImport`` does not remove
+modules imported during the context.