File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -919,6 +919,23 @@ def run_doctest(module, verbosity=None):
919919 (module .__name__ , t ))
920920 return f , t
921921
922+
923+ #=======================================================================
924+ # Support for saving and restoring the imported modules.
925+
926+ def modules_setup ():
927+ return sys .modules .copy (),
928+
929+ def modules_cleanup (oldmodules ):
930+ # Encoders/decoders are registered permanently within the internal
931+ # codec cache. If we destroy the corresponding modules their
932+ # globals will be set to None which will trip up the cached functions.
933+ encodings = [(k , v ) for k , v in sys .modules .items ()
934+ if k .startswith ('encodings.' )]
935+ sys .modules .clear ()
936+ sys .modules .update (encodings )
937+ sys .modules .update (oldmodules )
938+
922939#=======================================================================
923940# Threading support to prevent reporting refleaks when running regrtest.py -R
924941
Original file line number Diff line number Diff line change @@ -143,15 +143,14 @@ def setUp(self):
143143 self .meta_path = sys .meta_path [:]
144144 self .path_hooks = sys .path_hooks [:]
145145 sys .path_importer_cache .clear ()
146- self .modules_before = sys . modules . copy ()
146+ self .modules_before = support . modules_setup ()
147147
148148 def tearDown (self ):
149149 sys .path [:] = self .path
150150 sys .meta_path [:] = self .meta_path
151151 sys .path_hooks [:] = self .path_hooks
152152 sys .path_importer_cache .clear ()
153- sys .modules .clear ()
154- sys .modules .update (self .modules_before )
153+ support .modules_cleanup (* self .modules_before )
155154
156155
157156class ImportHooksTestCase (ImportHooksBaseTestCase ):
Original file line number Diff line number Diff line change @@ -48,13 +48,11 @@ def setUp(self):
4848 self .root = None
4949 self .pkgname = None
5050 self .syspath = list (sys .path )
51- self .sysmodules = sys . modules . copy ()
51+ self .modules_before = support . modules_setup ()
5252
5353 def tearDown (self ):
5454 sys .path [:] = self .syspath
55- sys .modules .clear ()
56- sys .modules .update (self .sysmodules )
57- del self .sysmodules
55+ support .modules_cleanup (* self .modules_before )
5856 cleanout (self .root )
5957
6058 # delete all modules concerning the tested hiearchy
Original file line number Diff line number Diff line change @@ -144,6 +144,11 @@ Extension Modules
144144Tests
145145-----
146146
147+ - Issue #6551: test_zipimport could import and then destroy some modules of
148+ the encodings package, which would make other tests fail further down
149+ the road because the internally cached encoders and decoders would point
150+ to empty global variables.
151+
147152- Issue #7295: Do not use a hardcoded file name in test_tarfile.
148153
149154- Issue #7270: Add some dedicated unit tests for multi-thread synchronization
You can’t perform that action at this time.
0 commit comments