8000 bpo-46659: Update the test on the mbcs codec alias (GH-31168) · python/cpython@04dd60e · GitHub
[go: up one dir, main page]

Skip to content

Commit 04dd60e

Browse files
authored
bpo-46659: Update the test on the mbcs codec alias (GH-31168)
encodings registers the _alias_mbcs() codec search function before the search_function() codec search function. Previously, the _alias_mbcs() was never used. Fix the test_codecs.test_mbcs_alias() test: use the current ANSI code page, not a fake ANSI code page number. Remove the test_site.test_aliasing_mbcs() test: the alias is now implemented in the encodings module, no longer in the site module.
1 parent 3da5526 commit 04dd60e

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

Lib/encodings/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ 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-
158155
if sys.platform == 'win32':
159156
def _alias_mbcs(encoding):
160157
try:
@@ -167,4 +164,8 @@ def _alias_mbcs(encoding):
167164
# Imports may fail while we are shutting down
168165
pass
169166

167+
# It must be registered before search_function()
170168
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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,10 @@ def test_basics(self):
19041904
name += "_codec"
19051905
elif encoding == "latin_1":
19061906
name = "latin_1"
1907-
self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-"))
1907+
# Skip the mbcs alias on Windows
1908+
if name != "mbcs":
1909+
self.assertEqual(encoding.replace("_", "-"),
1910+
name.replace("_", "-"))
19081911

19091912
(b, size) = codecs.getencoder(encoding)(s)
19101913
self.assertEqual(size, len(s), "encoding=%r" % encoding)
@@ -3188,11 +3191,13 @@ def test_incremental(self):
31883191
self.assertEqual(decoded, ('abc', 3))
31893192

31903193
def test_mbcs_alias(self):
3191-
# Check that looking up our 'default' codepage will return
3192-
# mbcs when we don't have a more specific one available
3193-
with mock.patch('_winapi.GetACP', return_value=123):
3194-
codec = codecs.lookup('cp123')
3195-
self.assertEqual(codec.name, 'mbcs')
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")
31963201

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

Lib/test/test_site.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,6 @@ def test_setting_help(self):
456456
# 'help' should be set in builtins
457457
self.assertTrue(hasattr(builtins, "help"))
458458

459-
def test_aliasing_mbcs(self):
460-
if sys.platform == "win32":
461-
import locale
462-
if locale.getdefaultlocale()[1].startswith('cp'):
463-
for value in encodings.aliases.aliases.values():
464-
if value == "mbcs":
465-
break
466-
else:
467-
self.fail("did not alias mbcs")
468-
469459
def test_sitecustomize_executed(self):
470460
# If sitecustomize is available, it should have been imported.
471461
if "sitecustomize" not in sys.modules:

0 commit comments

Comments
 (0)
0