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
13 changes: 11 additions & 2 deletions Lib/encodings/uu_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ def uu_decode(input, errors='strict'):
s = readline()
if not s:
raise ValueError('Missing "begin" line in input data')
if s[:5] == b'begin':
break
# Only a "begin <octal mode>" line, optionally followed by a
# filename, starts the encoded data (as in
# email.message._decode_uu()); anything else is leading text.
if s[:6] != b'begin ':
continue
mode = s[6:].split(b' ', 1)[0]
try:
int(mode, 8)
except ValueError:
continue
break

# Decode
while True:
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3251,6 +3251,27 @@ def test_uu_invalid(self):
# Missing "begin" line
self.assertRaises(ValueError, codecs.decode, b"", "uu-codec")

def test_uu_begin_line(self):
# Only a "begin <octal mode> [<filename>]" line starts the encoded
# data; other lines starting with "begin" are leading text.
block = codecs.encode(b"data", "uu-codec")
other = codecs.encode(b"other", "uu-codec").partition(b"\n")[2]
for line in (b"begin 666 name", b"begin 0 name with spaces",
b"begin 644 name\r", b"begin 666"):
with self.subTest(line=line):
encoded = line + b"\n" + other
self.assertEqual(codecs.decode(encoded + block, "uu-codec"),
b"other")
for line in (b"beginning of the text", b"begin", b"begin ",
b"begin mode name", b"begin 888 name",
b"beginx 666 name", b"begin-base64 666 name"):
with self.subTest(line=line):
encoded = line + b"\n" + other
self.assertRaises(ValueError, codecs.decode,
encoded, "uu-codec")
self.assertEqual(codecs.decode(encoded + block, "uu-codec"),
b"data")


# The codec system tries to add notes to exceptions in order to ensure
# the error mentions the operation being performed and the codec involved.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix the ``uu`` codec decoder taking any line that starts with ``begin``, such
as ``beginning of the report``, as the header of the encoded data. Like the
:mod:`email` package, it now requires ``begin`` followed by an octal mode and
skips other lines as leading text.
Loading