[Python-checkins] r76240 - in python/branches/py3k: Lib/test/support.py Lib/test/test_importhooks.py Lib/test/test_pkg.py Misc/NEWS

antoine.pitrou python-checkins at python.org
Fri Nov 13 17:29:04 CET 2009


Author: antoine.pitrou
Date: Fri Nov 13 17:29:04 2009
New Revision: 76240

Log:
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.



Modified:
   python/branches/py3k/Lib/test/support.py
   python/branches/py3k/Lib/test/test_importhooks.py
   python/branches/py3k/Lib/test/test_pkg.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/test/support.py
==============================================================================
--- python/branches/py3k/Lib/test/support.py	(original)
+++ python/branches/py3k/Lib/test/support.py	Fri Nov 13 17:29:04 2009
@@ -951,6 +951,23 @@
               (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
 

Modified: python/branches/py3k/Lib/test/test_importhooks.py
==============================================================================
--- python/branches/py3k/Lib/test/test_importhooks.py	(original)
+++ python/branches/py3k/Lib/test/test_importhooks.py	Fri Nov 13 17:29:04 2009
@@ -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):

Modified: python/branches/py3k/Lib/test/test_pkg.py
==============================================================================
--- python/branches/py3k/Lib/test/test_pkg.py	(original)
+++ python/branches/py3k/Lib/test/test_pkg.py	Fri Nov 13 17:29:04 2009
@@ -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

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Nov 13 17:29:04 2009
@@ -373,6 +373,11 @@
 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 #7270: Add some dedicated unit tests for multi-thread synchronization


More information about the Python-checkins mailing list