Index: Lib/test/test_importhooks.py =================================================================== --- Lib/test/test_importhooks.py (révision 76228) +++ Lib/test/test_importhooks.py (copie de travail) @@ -143,15 +143,14 @@ self.meta_path = sys.meta_path[:] self.path_hooks = sys.path_hooks[:] sys.path_importer_cache.clear() - self.modules_before = sys.modules.copy() + self.modules_before = support.modules_setup() def tearDown(self): sys.path[:] = self.path sys.meta_path[:] = self.meta_path sys.path_hooks[:] = self.path_hooks sys.path_importer_cache.clear() - sys.modules.clear() - sys.modules.update(self.modules_before) + support.modules_cleanup(*self.modules_before) class ImportHooksTestCase(ImportHooksBaseTestCase): Index: Lib/test/test_pkg.py =================================================================== --- Lib/test/test_pkg.py (révision 76228) +++ Lib/test/test_pkg.py (copie de travail) @@ -48,13 +48,11 @@ self.root = None self.pkgname = None self.syspath = list(sys.path) - self.sysmodules = sys.modules.copy() + self.modules_before = support.modules_setup() def tearDown(self): sys.path[:] = self.syspath - sys.modules.clear() - sys.modules.update(self.sysmodules) - del self.sysmodules + support.modules_cleanup(*self.modules_before) cleanout(self.root) # delete all modules concerning the tested hiearchy Index: Lib/test/support.py =================================================================== --- Lib/test/support.py (révision 76228) +++ Lib/test/support.py (copie de travail) @@ -951,7 +951,24 @@ (module.__name__, t)) return f, t + #======================================================================= +# Support for saving and restoring the imported modules. + +def modules_setup(): + return sys.modules.copy(), + +def modules_cleanup(oldmodules): + # Encoders/decoders are registered permanently within the internal + # codec cache. If we destroy the corresponding modules their + # globals will be set to None which will trip up the cached functions. + encodings = [(k, v) for k, v in sys.modules.items() + if k.startswith('encodings.')] + sys.modules.clear() + sys.modules.update(encodings) + sys.modules.update(oldmodules) + +#======================================================================= # Threading support to prevent reporting refleaks when running regrtest.py -R # NOTE: we use thread._count() rather than threading.enumerate() (or the