|
10 | 10 | from .._compat import ABC, Path, PurePath, FileNotFoundError
|
11 | 11 | from ..abc import ResourceReader
|
12 | 12 |
|
| 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 | + |
13 | 37 |
|
14 | 38 | try:
|
15 | 39 | from importlib.machinery import ModuleSpec
|
@@ -180,6 +204,10 @@ def tearDownClass(cls):
|
180 | 204 | except AttributeError:
|
181 | 205 | pass
|
182 | 206 |
|
| 207 | + def setUp(self): |
| 208 | + modules = modules_setup() |
| 209 | + self.addCleanup(modules_cleanup, *modules) |
| 210 | + |
183 | 211 |
|
184 | 212 | class ZipSetup(ZipSetupBase):
|
185 | 213 | ZIP_MODULE = zipdata01 # type: ignore
|
0 commit comments