Skip to content

Commit a4f0d6e

Browse files
committed
Backport fix for test isolation from Python 3.8/3.7. Closes python#61
1 parent fdc4ac4 commit a4f0d6e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

importlib_resources/docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
importlib_resources NEWS
33
==========================
44

5+
1.0 (2018-XX-XX)
6+
================
7+
* Backport fix for test isolation from Python 3.8/3.7. Closes #61
8+
59
0.8 (2018-05-17)
610
================
711
* Strip ``importlib_resources.__version__``. Closes #56

importlib_resources/tests/util.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@
1010
from .._compat import ABC, Path, PurePath, FileNotFoundError
1111
from ..abc import ResourceReader
1212

13+
try:
14+
from test.support import modules_setup, modules_cleanup
15+
except ImportError:
16+
# Python 2.7.
17+
def modules_setup():
18+
return sys.modules.copy(),
19+
20+
def modules_cleanup(oldmodules):
21+
# Encoders/decoders are registered permanently within the internal
22+
# codec cache. If we destroy the corresponding modules their
23+
# globals will be set to None which will trip up the cached functions.
24+
encodings = [(k, v) for k, v in sys.modules.items()
25+
if k.startswith('encodings.')]
26+
sys.modules.clear()
27+
sys.modules.update(encodings)
28+
# XXX: This kind of problem can affect more than just encodings. In
29+
# particular extension modules (such as _ssl) don't cope with reloading
30+
# properly. Really, test modules should be cleaning out the test
31+
# specific modules they know they added (ala test_runpy) rather than
32+
# relying on this function (as test_importhooks and test_pkg do
33+
# currently). Implicitly imported *real* modules should be left alone
34+
# (see issue 10556).
35+
sys.modules.update(oldmodules)
36+
1337

1438
try:
1539
from importlib.machinery import ModuleSpec
@@ -180,6 +204,10 @@ def tearDownClass(cls):
180204
except AttributeError:
181205
pass
182206

207+
def setUp(self):
208+
modules = modules_setup()
209+
self.addCleanup(modules_cleanup, *modules)
210+
183211

184212
class ZipSetup(ZipSetupBase):
185213
ZIP_MODULE = zipdata01 # type: ignore

0 commit comments

Comments
 (0)