Skip to content

Commit ccbe804

Browse files
authored
bpo-46659: Fix the MBCS codec alias on Windows (GH-31218)
1 parent 8fb9489 commit ccbe804

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

Lib/encodings/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@ def search_function(encoding):
152152
# Return the registry entry
153153
return entry
154154

155+
# Register the search_function in the Python codec registry
156+
codecs.register(search_function)
157+
155158
if sys.platform == 'win32':
159+
# bpo-671666, bpo-46668: If Python does not implement a codec for current
160+
# Windows ANSI code page, use the "mbcs" codec instead:
161+
# WideCharToMultiByte() and MultiByteToWideChar() functions with CP_ACP.
162+
# Python does not support custom code pages.
156163
def _alias_mbcs(encoding):
157164
try:
158165
import _winapi
@@ -164,8 +171,4 @@ def _alias_mbcs(encoding):
164171
# Imports may fail while we are shutting down
165172
pass
166173

167-
# It must be registered before search_function()
168174
codecs.register(_alias_mbcs)
169-
170-
# Register the search_function in the Python codec registry
171-
codecs.register(search_function)

Lib/test/test_codecs.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -3191,13 +3191,16 @@ def test_incremental(self):
31913191
self.assertEqual(decoded, ('abc', 3))
31923192

31933193
def test_mbcs_alias(self):
3194-
# On Windows, the encoding name must be the ANSI code page
3195-
encoding = locale.getpreferredencoding(False)
3196-
self.assertTrue(encoding.startswith('cp'), encoding)
3197-
3198-
# The encodings module create a "mbcs" alias to the ANSI code page
3199-
codec = codecs.lookup(encoding)
3200-
self.assertEqual(codec.name, "mbcs")
3194+
# Check that looking up our 'default' codepage will return
3195+
# mbcs when we don't have a more specific one available
3196+
code_page = 99_999
3197+
name = f'cp{code_page}'
3198+
with mock.patch('_winapi.GetACP', return_value=code_page):
3199+
try:
3200+
codec = codecs.lookup(name)
3201+
self.assertEqual(codec.name, 'mbcs')
3202+
finally:
3203+
codecs.unregister(name)
32013204

32023205
@support.bigmemtest(size=2**31, memuse=7, dry_run=False)
32033206
def test_large_input(self, size):

0 commit comments

Comments
 (0)