From 8eb207101d80b378abcbe4152a790505fcc8ac7c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 6 Mar 2024 15:33:15 -0500 Subject: [PATCH 1/7] Move test compatibility modules into a compat package. --- importlib_resources/tests/compat/__init__.py | 0 importlib_resources/tests/{_compat.py => compat/py39.py} | 9 ++++++--- importlib_resources/tests/test_custom.py | 2 +- importlib_resources/tests/test_files.py | 2 +- importlib_resources/tests/util.py | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 importlib_resources/tests/compat/__init__.py rename importlib_resources/tests/{_compat.py => compat/py39.py} (89%) diff --git a/importlib_resources/tests/compat/__init__.py b/importlib_resources/tests/compat/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/importlib_resources/tests/_compat.py b/importlib_resources/tests/compat/py39.py similarity index 89% rename from importlib_resources/tests/_compat.py rename to importlib_resources/tests/compat/py39.py index e7bf06dd..946280d1 100644 --- a/importlib_resources/tests/_compat.py +++ b/importlib_resources/tests/compat/py39.py @@ -1,10 +1,14 @@ +""" +Backward-compatability shims to support Python 3.9 and earlier. +""" + import os try: from test.support import import_helper # type: ignore except ImportError: - # Python 3.9 and earlier + class import_helper: # type: ignore from test.support import ( modules_setup, @@ -17,13 +21,12 @@ class import_helper: # type: ignore try: from test.support import os_helper # type: ignore except ImportError: - # Python 3.9 compat + class os_helper: # type:ignore from test.support import temp_dir try: - # Python 3.10 from test.support.os_helper import unlink except ImportError: from test.support import unlink as _unlink diff --git a/importlib_resources/tests/test_custom.py b/importlib_resources/tests/test_custom.py index 35336f8f..86c65676 100644 --- a/importlib_resources/tests/test_custom.py +++ b/importlib_resources/tests/test_custom.py @@ -6,7 +6,7 @@ from .. import abc from ..abc import TraversableResources, ResourceReader from . import util -from ._compat import os_helper +from .compat.py39 import os_helper class SimpleLoader: diff --git a/importlib_resources/tests/test_files.py b/importlib_resources/tests/test_files.py index 51ecc896..87290340 100644 --- a/importlib_resources/tests/test_files.py +++ b/importlib_resources/tests/test_files.py @@ -9,7 +9,7 @@ from . import data01 from . import util from . import _path -from ._compat import os_helper, import_helper +from .compat.py39 import os_helper, import_helper @contextlib.contextmanager diff --git a/importlib_resources/tests/util.py b/importlib_resources/tests/util.py index 066f4113..ee0aa0ad 100644 --- a/importlib_resources/tests/util.py +++ b/importlib_resources/tests/util.py @@ -8,7 +8,7 @@ from . import data01 from ..abc import ResourceReader -from ._compat import import_helper, os_helper +from .compat.py39 import import_helper, os_helper from . import zip as zip_ From f3f4b0ab8b50297e33343cc5e51b15dcf08c2412 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 6 Mar 2024 10:58:25 -0500 Subject: [PATCH 2/7] 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. --- importlib_resources/tests/compat/py312.py | 18 ++++++++++++++++++ importlib_resources/tests/compat/py39.py | 1 - importlib_resources/tests/test_files.py | 5 +++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 importlib_resources/tests/compat/py312.py diff --git a/importlib_resources/tests/compat/py312.py b/importlib_resources/tests/compat/py312.py new file mode 100644 index 00000000..ea9a58ba --- /dev/null +++ b/importlib_resources/tests/compat/py312.py @@ -0,0 +1,18 @@ +import contextlib + +from .py39 import import_helper + + +@contextlib.contextmanager +def isolated_modules(): + """ + Save modules on entry and cleanup on exit. + """ + (saved,) = import_helper.modules_setup() + try: + yield + finally: + import_helper.modules_cleanup(saved) + + +vars(import_helper).setdefault('isolated_modules', isolated_modules) diff --git a/importlib_resources/tests/compat/py39.py b/importlib_resources/tests/compat/py39.py index 946280d1..3b4965bc 100644 --- a/importlib_resources/tests/compat/py39.py +++ b/importlib_resources/tests/compat/py39.py @@ -14,7 +14,6 @@ class import_helper: # type: ignore modules_setup, modules_cleanup, DirsOnSysPath, - CleanImport, ) diff --git a/importlib_resources/tests/test_files.py b/importlib_resources/tests/test_files.py index 87290340..9e45f701 100644 --- a/importlib_resources/tests/test_files.py +++ b/importlib_resources/tests/test_files.py @@ -9,7 +9,8 @@ from . import data01 from . import util from . import _path -from .compat.py39 import os_helper, import_helper +from .compat.py39 import os_helper +from .compat.py312 import import_helper @contextlib.contextmanager @@ -68,7 +69,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): From 414e4c0919b0b15d3546aee94d73835c7b24bdc3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 6 Mar 2024 16:02:18 -0500 Subject: [PATCH 3/7] Use a SimpleNamespace for setdefault compatibility. --- importlib_resources/tests/compat/py39.py | 12 ++++++------ setup.cfg | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/importlib_resources/tests/compat/py39.py b/importlib_resources/tests/compat/py39.py index 3b4965bc..a1e1e2d0 100644 --- a/importlib_resources/tests/compat/py39.py +++ b/importlib_resources/tests/compat/py39.py @@ -3,18 +3,18 @@ """ import os +import types + +from jaraco.collections import Projection try: from test.support import import_helper # type: ignore except ImportError: + import test.support - class import_helper: # type: ignore - from test.support import ( - modules_setup, - modules_cleanup, - DirsOnSysPath, - ) + names = 'modules_setup', 'modules_cleanup', 'DirsOnSysPath' + import_helper = types.SimpleNamespace(**Projection(names, vars(test.support))) try: diff --git a/setup.cfg b/setup.cfg index 017cdc48..feef5904 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,6 +34,7 @@ testing = # local zipp >= 3.17 + jaraco.collections docs = # upstream From a8892ae076dd24f129e51125f28b9a9abf846e8f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 6 Mar 2024 16:07:41 -0500 Subject: [PATCH 4/7] Extract 'from_test_support' helper. --- importlib_resources/tests/compat/py39.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/importlib_resources/tests/compat/py39.py b/importlib_resources/tests/compat/py39.py index a1e1e2d0..93fb699c 100644 --- a/importlib_resources/tests/compat/py39.py +++ b/importlib_resources/tests/compat/py39.py @@ -8,21 +8,27 @@ from jaraco.collections import Projection +def from_test_support(*names): + """ + Return a SimpleNamespace of names from test.support. + """ + import test.support + + return types.SimpleNamespace(**Projection(names, vars(test.support))) + + try: from test.support import import_helper # type: ignore except ImportError: - import test.support - - names = 'modules_setup', 'modules_cleanup', 'DirsOnSysPath' - import_helper = types.SimpleNamespace(**Projection(names, vars(test.support))) + import_helper = from_test_support( + 'modules_setup', 'modules_cleanup', 'DirsOnSysPath' + ) try: from test.support import os_helper # type: ignore except ImportError: - - class os_helper: # type:ignore - from test.support import temp_dir + os_helper = from_test_support('temp_dir') try: From 8918e27bc55dd0a71661b061d6371bd0fd3c49fe Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 6 Mar 2024 16:11:29 -0500 Subject: [PATCH 5/7] Remove unlink compatibility (unused). --- importlib_resources/tests/compat/py39.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/importlib_resources/tests/compat/py39.py b/importlib_resources/tests/compat/py39.py index 93fb699c..81ec9aac 100644 --- a/importlib_resources/tests/compat/py39.py +++ b/importlib_resources/tests/compat/py39.py @@ -2,7 +2,6 @@ Backward-compatability shims to support Python 3.9 and earlier. """ -import os import types from jaraco.collections import Projection @@ -29,12 +28,3 @@ def from_test_support(*names): from test.support import os_helper # type: ignore except ImportError: os_helper = from_test_support('temp_dir') - - -try: - from test.support.os_helper import unlink -except ImportError: - from test.support import unlink as _unlink - - def unlink(target): - return _unlink(os.fspath(target)) From 99a41c1fe19aa9f39ec88a69a66a6298837db4d2 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 6 Mar 2024 16:17:08 -0500 Subject: [PATCH 6/7] Re-use isolated_modules in ZipSetupBase. --- importlib_resources/tests/util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/importlib_resources/tests/util.py b/importlib_resources/tests/util.py index ee0aa0ad..fb827d2f 100644 --- a/importlib_resources/tests/util.py +++ b/importlib_resources/tests/util.py @@ -148,8 +148,7 @@ def setUp(self): self.fixtures = contextlib.ExitStack() self.addCleanup(self.fixtures.close) - modules = import_helper.modules_setup() - self.addCleanup(import_helper.modules_cleanup, *modules) + self.fixtures.enter_context(import_helper.isolated_modules()) temp_dir = self.fixtures.enter_context(os_helper.temp_dir()) modules = pathlib.Path(temp_dir) / 'zipped modules.zip' From 56bd2b9cb41f76db5b7ef057cadd6d056aadf21f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 7 Mar 2024 08:10:44 -0500 Subject: [PATCH 7/7] Finalize --- NEWS.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.rst b/NEWS.rst index dbfc2227..adc804c7 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -1,3 +1,9 @@ +v6.1.3 +====== + +No significant changes. + + v6.1.2 ======