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.
This commit is contained in:
Antoine Pitrou 2009-11-13 16:29:04 +00:00
parent 88d1bc448b
commit 060cee221b
4 changed files with 26 additions and 7 deletions

View file

@ -951,6 +951,23 @@ def run_doctest(module, verbosity=None):
(module.__name__, t)) (module.__name__, t))
return f, 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 # Threading support to prevent reporting refleaks when running regrtest.py -R

View file

@ -143,15 +143,14 @@ class ImportHooksBaseTestCase(unittest.TestCase):
self.meta_path = sys.meta_path[:] self.meta_path = sys.meta_path[:]
self.path_hooks = sys.path_hooks[:] self.path_hooks = sys.path_hooks[:]
sys.path_importer_cache.clear() sys.path_importer_cache.clear()
self.modules_before = sys.modules.copy() self.modules_before = support.modules_setup()
def tearDown(self): def tearDown(self):
sys.path[:] = self.path sys.path[:] = self.path
sys.meta_path[:] = self.meta_path sys.meta_path[:] = self.meta_path
sys.path_hooks[:] = self.path_hooks sys.path_hooks[:] = self.path_hooks
sys.path_importer_cache.clear() sys.path_importer_cache.clear()
sys.modules.clear() support.modules_cleanup(*self.modules_before)
sys.modules.update(self.modules_before)
class ImportHooksTestCase(ImportHooksBaseTestCase): class ImportHooksTestCase(ImportHooksBaseTestCase):

View file

@ -48,13 +48,11 @@ class TestPkg(unittest.TestCase):
self.root = None self.root = None
self.pkgname = None self.pkgname = None
self.syspath = list(sys.path) self.syspath = list(sys.path)
self.sysmodules = sys.modules.copy() self.modules_before = support.modules_setup()
def tearDown(self): def tearDown(self):
sys.path[:] = self.syspath sys.path[:] = self.syspath
sys.modules.clear() support.modules_cleanup(*self.modules_before)
sys.modules.update(self.sysmodules)
del self.sysmodules
cleanout(self.root) cleanout(self.root)
# delete all modules concerning the tested hiearchy # delete all modules concerning the tested hiearchy

View file

@ -373,6 +373,11 @@ Documentation
Tests Tests
----- -----
- 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.
- Issue #7295: Do not use a hardcoded file name in test_tarfile. - Issue #7295: Do not use a hardcoded file name in test_tarfile.
- Issue #7270: Add some dedicated unit tests for multi-thread synchronization - Issue #7270: Add some dedicated unit tests for multi-thread synchronization