Skip to content

Commit c8fc658

Browse files
deprecate non-ascii
1 parent 5f91d5d commit c8fc658

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

Doc/deprecations/pending-removal-in-3.17.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ Pending removal in Python 3.17
2323
(Contributed by Shantanu Jain in :gh:`91896`.)
2424

2525

26+
* :mod:`encodings`:
27+
28+
- Passing non-ascii *encoding* names to :func:`encodings.normalize_encoding`
29+
is deprecated and scheduled for removal in Python 3.17.
30+
(Contributed by Stan Ulbrych in :gh:`136702`)
31+
2632
* :mod:`typing`:
2733

2834
- Before Python 3.14, old-style unions were implemented using the private class

Lib/encodings/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
2727
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
2828
29-
"""#"
29+
"""
3030

3131
import codecs
32+
import warnings
3233
import sys
3334
from . import aliases
3435

@@ -55,6 +56,11 @@ def normalize_encoding(encoding):
5556
if isinstance(encoding, bytes):
5657
encoding = str(encoding, "ascii")
5758

59+
if not encoding.isascii():
60+
warnings.warn(
61+
"Support for non-ascii encoding names will be removed in 3.17",
62+
DeprecationWarning, stacklevel=2)
63+
5864
chars = []
5965
punct = False
6066
for c in encoding:

Lib/test/test_codecs.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,22 +3886,26 @@ def search_function(encoding):
38863886
self.assertEqual(codecs.lookup('TEST.AAA 8'), ('test.aaa-8', 2, 3, 4))
38873887
self.assertEqual(codecs.lookup('TEST.AAA---8'), ('test.aaa---8', 2, 3, 4))
38883888
self.assertEqual(codecs.lookup('TEST.AAA 8'), ('test.aaa---8', 2, 3, 4))
3889-
self.assertEqual(codecs.lookup('TEST.AAA\xe9\u20ac-8'), ('test.aaa\xe9\u20ac-8', 2, 3, 4))
38903889
self.assertEqual(codecs.lookup('TEST.AAA.8'), ('test.aaa.8', 2, 3, 4))
38913890
self.assertEqual(codecs.lookup('TEST.AAA...8'), ('test.aaa...8', 2, 3, 4))
3891+
with self.assertWarns(DeprecationWarning):
3892+
self.assertEqual(codecs.lookup('TEST.AAA\xe9\u20ac-8'), ('test.aaa\xe9\u20ac-8', 2, 3, 4))
38923893

38933894
def test_encodings_normalize_encoding(self):
3894-
# encodings.normalize_encoding() ignores non-ASCII characters.
38953895
normalize = encodings.normalize_encoding
38963896
self.assertEqual(normalize('utf_8'), 'utf_8')
3897-
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
38983897
self.assertEqual(normalize('utf 8'), 'utf_8')
38993898
# encodings.normalize_encoding() doesn't convert
39003899
# characters to lower case.
39013900
self.assertEqual(normalize('UTF 8'), 'UTF_8')
39023901
self.assertEqual(normalize('utf.8'), 'utf.8')
39033902
self.assertEqual(normalize('utf...8'), 'utf...8')
39043903

3904+
# Non-ASCII *encoding* is deprecated.
3905+
with self.assertWarnsRegex(DeprecationWarning,
3906+
"Support for non-ascii encoding names will be removed in 3.17"):
3907+
self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
3908+
39053909

39063910
if __name__ == "__main__":
39073911
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`encodings`: Deprecate passing a non-ascii *encoding* name to
2+
:func:`encodings.normalize_encoding` and schedule removal of support for
3+
Python 3.17.

0 commit comments

Comments
 (0)