Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca15409

Browse files
committed
Merged revisions 76240 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r76240 | antoine.pitrou | 2009-11-13 17:29:04 +0100 (ven., 13 nov. 2009) | 6 lines Issue #6551: test_zipimport could import and then destroy some modules of the encodings package, which would make other tests fail further down the road because the internally cached encoders and decoders would point to empty global variables. ........
1 parent 8f6713f commit ca15409

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

Lib/test/support.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff 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

Lib/test/test_importhooks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff 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

157156
class ImportHooksTestCase(ImportHooksBaseTestCase):

Lib/test/test_pkg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ Extension Modules
144144
Tests
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

0 commit comments

Comments
 (0)