Skip to content

Commit

Permalink
gh-116307: Create a new import helper 'isolated modules' and use that…
Browse files Browse the repository at this point in the history
… instead of 'Clean Import' to ensure that tests from importlib_resources don't leave modules in sys.modules.
  • Loading branch information
jaraco committed Mar 6, 2024
1 parent 8eb2071 commit f3f4b0a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions importlib_resources/tests/compat/py312.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion importlib_resources/tests/compat/py39.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class import_helper: # type: ignore
modules_setup,
modules_cleanup,
DirsOnSysPath,
CleanImport,
)


Expand Down
5 changes: 3 additions & 2 deletions importlib_resources/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f3f4b0a

Please sign in to comment.