From f424c82703da2fae56490ce4c9af9e5f49af775a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 6 Feb 2022 19:38:56 +0100 Subject: [PATCH 1/2] bpo-46659: Fix the mbcs codec alias 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. --- Lib/encodings/__init__.py | 7 ++++--- Lib/test/test_codecs.py | 12 +++++++----- Lib/test/test_site.py | 10 ---------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 4b37d3321c9033..dff22a4b27f452 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -152,9 +152,6 @@ 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': def _alias_mbcs(encoding): try: @@ -167,4 +164,8 @@ 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 4ad24dbb9a9243..1d33598377cf5c 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3188,11 +3188,13 @@ def test_incremental(self): self.assertEqual(decoded, ('abc', 3)) def test_mbcs_alias(self): - # Check that looking up our 'default' codepage will return - # mbcs when we don't have a more specific one available - with mock.patch('_winapi.GetACP', return_value=123): - codec = codecs.lookup('cp123') - self.assertEqual(codec.name, 'mbcs') + # 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") @support.bigmemtest(size=2**31, memuse=7, dry_run=False) def test_large_input(self, size): diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 032a1be3aa5290..a67cfec72aee4d 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -456,16 +456,6 @@ def test_setting_help(self): # 'help' should be set in builtins self.assertTrue(hasattr(builtins, "help")) - def test_aliasing_mbcs(self): - if sys.platform == "win32": - import locale - if locale.getdefaultlocale()[1].startswith('cp'): - for value in encodings.aliases.aliases.values(): - if value == "mbcs": - break - else: - self.fail("did not alias mbcs") - def test_sitecustomize_executed(self): # If sitecustomize is available, it should have been imported. if "sitecustomize" not in sys.modules: From 6a4c88670b5a908a33253a24760c7ae96701de38 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 6 Feb 2022 21:28:09 +0100 Subject: [PATCH 2/2] Update test_codecs.test_basics() --- Lib/test/test_codecs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 1d33598377cf5c..d30ff8f82db9a5 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1904,7 +1904,10 @@ def test_basics(self): name += "_codec" elif encoding == "latin_1": name = "latin_1" - self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-")) + # Skip the mbcs alias on Windows + if name != "mbcs": + self.assertEqual(encoding.replace("_", "-"), + name.replace("_", "-")) (b, size) = codecs.getencoder(encoding)(s) self.assertEqual(size, len(s), "encoding=%r" % encoding)