From 4313d6a717034f6bf374686e79cd6b2087a930c1 Mon Sep 17 00:00:00 2001 From: qventymr <80840718+qventymr@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:55:49 +0300 Subject: [PATCH 1/2] gh-162462: Require canonical Windows code page names Validate the numeric suffix before converting it to a code page number. The codec registry caches successful lookups by name, so alternate spellings of the same number can create duplicate codec entries. --- Doc/library/codecs.rst | 4 +++- Lib/encodings/__init__.py | 8 +++++++- Lib/test/test_codecs.py | 10 ++++++++++ .../2026-11-23-17-51-17.gh-issue-162462.7bd940.rst | 2 ++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-23-17-51-17.gh-issue-162462.7bd940.rst diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index f19407818528e2..8d0305666c44da 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1618,7 +1618,9 @@ This module implements the following functions: .. function:: win32_code_page_search_function(encoding) - Search for a Windows code page encoding *encoding* of the form ``cpXXXX``. + Search for a Windows code page encoding *encoding* of the form ``cpXXXX``, + where ``XXXX`` is a non-negative integer written in ASCII decimal digits + without leading zeros. If the code page is valid and supported, return a :class:`codecs.CodecInfo` object for it. diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 04594eab8224b3..95335d7ccb8589 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -163,8 +163,14 @@ def win32_code_page_search_function(encoding): encoding = encoding.lower() if not encoding.startswith('cp'): return None + # The codec registry caches results by name. + code_page = encoding[2:] + if not code_page.isascii() or not code_page.isdecimal(): + return None + if code_page != '0' and code_page.startswith('0'): + return None try: - cp = int(encoding[2:]) + cp = int(code_page) except ValueError: return None # Test if the code page is supported diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 4492e4d874189f..24cc257f62e9f9 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3454,6 +3454,16 @@ def test_invalid_code_page(self): self.assertRaises(OSError, codecs.code_page_encode, 123, 'a') self.assertRaises(OSError, codecs.code_page_decode, 123, b'a') + def test_lookup_code_page(self): + # Code page 0 selects the current Windows ANSI code page. + self.assertEqual(codecs.lookup('CP0').name, 'cp0') + self.assertEqual(codecs.lookup('windows-1252').name, 'cp1252') + + def test_lookup_invalid_code_page_names(self): + for encoding in ('cp01252', 'cp1_252', 'cp+1252', 'cp-1'): + with self.subTest(encoding=encoding): + self.assertRaises(LookupError, codecs.lookup, encoding) + def test_code_page_name(self): self.assertRaisesRegex(UnicodeEncodeError, 'cp932', codecs.code_page_encode, 932, '\xff') diff --git a/Misc/NEWS.d/next/Library/2026-11-23-17-51-17.gh-issue-162462.7bd940.rst b/Misc/NEWS.d/next/Library/2026-11-23-17-51-17.gh-issue-162462.7bd940.rst new file mode 100644 index 00000000000000..c1d2209c71eb38 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-23-17-51-17.gh-issue-162462.7bd940.rst @@ -0,0 +1,2 @@ +Reject noncanonical ``cpXXX`` codec names in the Windows code page lookup, +including numbers with leading zeros, signs, or underscores. From 889727856e7b58330bbbc6286d734bb61d97cd20 Mon Sep 17 00:00:00 2001 From: qventymr <80840718+qventymr@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:40:03 +0300 Subject: [PATCH 2/2] gh-162462: Apply Windows codec review suggestions --- Doc/library/codecs.rst | 4 +--- Lib/test/test_codecs.py | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index 8d0305666c44da..f19407818528e2 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1618,9 +1618,7 @@ This module implements the following functions: .. function:: win32_code_page_search_function(encoding) - Search for a Windows code page encoding *encoding* of the form ``cpXXXX``, - where ``XXXX`` is a non-negative integer written in ASCII decimal digits - without leading zeros. + Search for a Windows code page encoding *encoding* of the form ``cpXXXX``. If the code page is valid and supported, return a :class:`codecs.CodecInfo` object for it. diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 24cc257f62e9f9..1606062a41e158 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -3460,6 +3460,7 @@ def test_lookup_code_page(self): self.assertEqual(codecs.lookup('windows-1252').name, 'cp1252') def test_lookup_invalid_code_page_names(self): + # gh-162462: only the canonical cpXXX spelling is accepted. for encoding in ('cp01252', 'cp1_252', 'cp+1252', 'cp-1'): with self.subTest(encoding=encoding): self.assertRaises(LookupError, codecs.lookup, encoding)