Skip to content

gh-136736: Fix handling alphanumerical non-ASCII characters in encodings.normalize_encoding() #136737

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
gh-136736: Fix handling alphanumerical non-ASCII characters in encodi…
…ngs.normalize_encoding()
  • Loading branch information
serhiy-storchaka committed Jul 17, 2025
commit 8baff4c5d2e88fb36fbd9d54c5376202c15b94c4
5 changes: 2 additions & 3 deletions Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ def normalize_encoding(encoding):
chars = []
punct = False
for c in encoding:
if c.isalnum() or c == '.':
if c.isascii() and (c.isalnum() or c == '.'):
if punct and chars:
chars.append('_')
if c.isascii():
chars.append(c)
chars.append(c)
punct = False
else:
punct = True
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3888,6 +3888,12 @@ def search_function(encoding):
self.assertEqual(FOUND, codecs.lookup('AAA---8'))
self.assertEqual(FOUND, codecs.lookup('AAA 8'))
self.assertEqual(FOUND, codecs.lookup('aaa\xe9\u20ac-8'))
self.assertEqual(FOUND, codecs.lookup('aaa\xe98'))
self.assertEqual(FOUND, codecs.lookup('aaa\u20ac8'))
self.assertEqual(FOUND, codecs.lookup('aaa-\xe9-8'))
self.assertEqual(FOUND, codecs.lookup('aaa-\u20ac-8'))
self.assertEqual(FOUND, codecs.lookup('aaa-8-\xe9'))
self.assertEqual(FOUND, codecs.lookup('aaa-8-\u20ac'))
self.assertEqual(NOT_FOUND, codecs.lookup('AAA.8'))
self.assertEqual(NOT_FOUND, codecs.lookup('AAA...8'))
self.assertEqual(NOT_FOUND, codecs.lookup('BBB-8'))
Expand All @@ -3899,6 +3905,12 @@ def test_encodings_normalize_encoding(self):
normalize = encodings.normalize_encoding
self.assertEqual(normalize('utf_8'), 'utf_8')
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
self.assertEqual(normalize('utf\xe98'), 'utf_8')
self.assertEqual(normalize('utf\u20ac8'), 'utf_8')
self.assertEqual(normalize('utf-\xe9-8'), 'utf_8')
self.assertEqual(normalize('utf-\u20ac-8'), 'utf_8')
self.assertEqual(normalize('utf-8-\xe9'), 'utf_8')
self.assertEqual(normalize('utf-8-\u20ac'), 'utf_8')
self.assertEqual(normalize('utf 8'), 'utf_8')
# encodings.normalize_encoding() doesn't convert
# characters to lower case.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix handling alphanumerical non-ASCII characters in
:func:`encodings.normalize_encoding`.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy