]> granicus.if.org Git - python/commitdiff
Issue #27959: Prevent ImportError from escaping codec search function
authorSteve Dower <steve.dower@microsoft.com>
Wed, 7 Sep 2016 16:31:52 +0000 (09:31 -0700)
committerSteve Dower <steve.dower@microsoft.com>
Wed, 7 Sep 2016 16:31:52 +0000 (09:31 -0700)
Lib/encodings/__init__.py
Lib/test/test_io.py

index 9a9b90b004055b5c1b503fd891f88b67d8ed3b6e..ca07881f345fa5617d79ddaccf78b4e4174a745c 100644 (file)
@@ -155,9 +155,13 @@ codecs.register(search_function)
 
 if sys.platform == 'win32':
     def _alias_mbcs(encoding):
-        import _bootlocale
-        if encoding == _bootlocale.getpreferredencoding(False):
-            import encodings.mbcs
-            return encodings.mbcs.getregentry()
+        try:
+            import _bootlocale
+            if encoding == _bootlocale.getpreferredencoding(False):
+                import encodings.mbcs
+                return encodings.mbcs.getregentry()
+        except ImportError:
+            # Imports may fail while we are shutting down
+            pass
 
     codecs.register(_alias_mbcs)
index f9f5d7a7ee8aa3f21f46793562e1cf56ea49e61a..c48ec3a23966952d8cec933788ecdbd1aee79c70 100644 (file)
@@ -3230,8 +3230,7 @@ def _to_memoryview(buf):
 
 class CTextIOWrapperTest(TextIOWrapperTest):
     io = io
-    shutdown_error = ("ImportError: sys.meta_path is None"
-        if os.name == "nt" else "RuntimeError: could not find io module state")
+    shutdown_error = "RuntimeError: could not find io module state"
 
     def test_initialization(self):
         r = self.BytesIO(b"\xc3\xa9\n\n")