Skip to content
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
5 changes: 4 additions & 1 deletion Lib/encodings/punycode.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ def punycode_decode(text, errors):
text = text.encode("ascii")
if isinstance(text, memoryview):
text = bytes(text)
pos = text.rfind(b"-")
# RFC 3492, section 6.2: the delimiter is only consumed if at least one
# basic code point precedes it. A leading "-" is left in the extended
# part, where it is an invalid digit.
pos = text.rfind(b"-", 1)
if pos == -1:
base = ""
extended = text.upper()
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,11 @@ def test_decode_invalid(self):
(b"Proprostnemluvesky-&uyb24dma41a", "strict", UnicodeDecodeError("punycode", b"", 19, 20, "")),
(b"Proprostnemluvesky-uyb24d&ma41a", "strict", UnicodeDecodeError("punycode", b"", 25, 26, "")),
(b"Proprostnemluvesky-uyb24dma41a&", "strict", UnicodeDecodeError("punycode", b"", 30, 31, "")),
# A delimiter not preceded by any basic code point (RFC 3492, 6.2)
(b"-", "strict", UnicodeDecodeError("punycode", b"", 0, 1, "")),
(b"-80a1acny", "strict", UnicodeDecodeError("punycode", b"", 0, 1, "")),
(b"xn--w&", "ignore", "xn-"),
(b"-80a1acny", "ignore", ""),
]
for puny, errors, expected in testcases:
with self.subTest(puny=puny, errors=errors):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the ``punycode`` codec accepting a delimiter (``-``) that is not preceded
by any basic code point. Such input, for example ``b'-80a1acny'``, now raises
:exc:`UnicodeDecodeError`, as required by :rfc:`3492`.
Loading