[Python-checkins] bpo-46659: Fix the MBCS codec alias on Windows (GH-31218)

vstinner webhook-mailer at python.org
Tue Feb 22 16:04:20 EST 2022


https://github.com/python/cpython/commit/ccbe8045faf6e63d36229ea4e1b9298572cda126
commit: ccbe8045faf6e63d36229ea4e1b9298572cda126
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-02-22T22:04:07+01:00
summary:

bpo-46659: Fix the MBCS codec alias on Windows (GH-31218)

files:
M Lib/encodings/__init__.py
M Lib/test/test_codecs.py

diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py
index dff22a4b27f45..f9075b8f0d98a 100644
--- a/Lib/encodings/__init__.py
+++ b/Lib/encodings/__init__.py
@@ -152,7 +152,14 @@ def search_function(encoding):
     # Return the registry entry
     return entry
 
+# Register the search_function in the Python codec registry
+codecs.register(search_function)
+
 if sys.platform == 'win32':
+    # bpo-671666, bpo-46668: If Python does not implement a codec for current
+    # Windows ANSI code page, use the "mbcs" codec instead:
+    # WideCharToMultiByte() and MultiByteToWideChar() functions with CP_ACP.
+    # Python does not support custom code pages.
     def _alias_mbcs(encoding):
         try:
             import _winapi
@@ -164,8 +171,4 @@ def _alias_mbcs(encoding):
             # Imports may fail while we are shutting down
             pass
 
-    # It must be registered before search_function()
     codecs.register(_alias_mbcs)
-
-# Register the search_function in the Python codec registry
-codecs.register(search_function)
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index d30ff8f82db9a..8118ec6f4727b 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -3191,13 +3191,16 @@ def test_incremental(self):
         self.assertEqual(decoded, ('abc', 3))
 
     def test_mbcs_alias(self):
-        # On Windows, the encoding name must be the ANSI code page
-        encoding = locale.getpreferredencoding(False)
-        self.assertTrue(encoding.startswith('cp'), encoding)
-
-        # The encodings module create a "mbcs" alias to the ANSI code page
-        codec = codecs.lookup(encoding)
-        self.assertEqual(codec.name, "mbcs")
+        # Check that looking up our 'default' codepage will return
+        # mbcs when we don't have a more specific one available
+        code_page = 99_999
+        name = f'cp{code_page}'
+        with mock.patch('_winapi.GetACP', return_value=code_page):
+            try:
+                codec = codecs.lookup(name)
+                self.assertEqual(codec.name, 'mbcs')
+            finally:
+                codecs.unregister(name)
 
     @support.bigmemtest(size=2**31, memuse=7, dry_run=False)
     def test_large_input(self, size):



More information about the Python-checkins mailing list