Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46659: Update the test on the mbcs codec alias #31168

Merged
merged 2 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
17 changes: 11 additions & 6 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -3188,11 +3191,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)
Comment on lines +3194 to +3196
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if PYTHONUTF8 is set in the environment, because it overrides getpreferredencoding(False) and _get_locale_encoding().


# The encodings module create a "mbcs" alias to the ANSI code page
codec = codecs.lookup(encoding)
self.assertEqual(codec.name, "mbcs")
Comment on lines +3198 to +3200
Copy link
Contributor

@eryksun eryksun Feb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was never true before. With 1252 as my ANSI code page, I checked codecs.lookup('cp1252') in 2.7, 3.4, 3.5, 3.6, 3.9, and 3.10, and none of them return the "mbcs" encoding. It's not equivalent, and not supposed to be. The implementation of "cp1252" should be cross-platform, regardless of whether we're on a Windows system with 1252 as the ANSI code page, as opposed to a Windows system with some other ANSI code page, or a Linux or macOS system.

The differences are that "mbcs" maps every byte, whereas our code-page encodings do not map undefined bytes, and the "replace" handler of "mbcs" uses a best-fit mapping (e.g. "α" -> "a") when encoding text, instead of mapping all undefined characters to "?".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is worse than what I expected, I created https://bugs.python.org/issue46668 to discuss it.


@support.bigmemtest(size=2**31, memuse=7, dry_run=False)
def test_large_input(self, size):
Expand Down
10 changes: 0 additions & 10 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down