mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Fix a more bytes/str confusion.
Use str.encode('raw-unicode-escape') consistently instead of bytes(string). Remove the convert_eols argument from base64mime.decode(). This matches previous API changes done to the quoprimime module.
This commit is contained in:
parent
ce36ad8a46
commit
8b3d659692
4 changed files with 20 additions and 24 deletions
|
@ -109,7 +109,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=False,
|
||||||
lines = []
|
lines = []
|
||||||
for line in base64ed:
|
for line in base64ed:
|
||||||
# Ignore the last character of each line if it is a newline
|
# Ignore the last character of each line if it is a newline
|
||||||
if line.endswith(NL):
|
if line[-1] == ord(NL):
|
||||||
line = line[:-1]
|
line = line[:-1]
|
||||||
# Add the chrome
|
# Add the chrome
|
||||||
lines.append('=?%s?b?%s?=' % (charset, line))
|
lines.append('=?%s?b?%s?=' % (charset, line))
|
||||||
|
@ -158,25 +158,19 @@ encodestring = encode
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def decode(s, convert_eols=False):
|
def decode(string):
|
||||||
"""Decode a raw base64 string, returning a bytes object.
|
"""Decode a raw base64 string, returning a bytes object.
|
||||||
|
|
||||||
If convert_eols is set to a string value, all canonical email linefeeds,
|
This function does not parse a full MIME header value encoded with base64
|
||||||
e.g. "\\r\\n", in the decoded text will be converted to the value of
|
(like =?iso-8895-1?b?bmloISBuaWgh?=) -- use the high level
|
||||||
convert_eols. os.linesep is a good choice for convert_eols if you are
|
email.Header class for that functionality.
|
||||||
decoding a text attachment.
|
|
||||||
|
|
||||||
This function does not parse a full MIME header value encoded with
|
|
||||||
base64 (like =?iso-8895-1?b?bmloISBuaWgh?=) -- please use the high
|
|
||||||
level email.Header class for that functionality.
|
|
||||||
"""
|
"""
|
||||||
if not s:
|
if not string:
|
||||||
return s
|
return bytes()
|
||||||
|
elif isinstance(string, str):
|
||||||
dec = a2b_base64(s)
|
return a2b_base64(string.encode('raw-unicode-escape'))
|
||||||
if convert_eols:
|
else:
|
||||||
return dec.replace(CRLF, convert_eols)
|
return a2b_base64(string)
|
||||||
return dec
|
|
||||||
|
|
||||||
|
|
||||||
# For convenience and backwards compatibility w/ standard base64 module
|
# For convenience and backwards compatibility w/ standard base64 module
|
||||||
|
|
|
@ -201,7 +201,7 @@ class Message:
|
||||||
# Incorrect padding
|
# Incorrect padding
|
||||||
pass
|
pass
|
||||||
elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
|
elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
|
||||||
in_file = BytesIO(bytes(payload + '\n'))
|
in_file = BytesIO((payload + '\n').encode('raw-unicode-escape'))
|
||||||
out_file = BytesIO()
|
out_file = BytesIO()
|
||||||
try:
|
try:
|
||||||
uu.decode(in_file, out_file, quiet=True)
|
uu.decode(in_file, out_file, quiet=True)
|
||||||
|
@ -757,7 +757,8 @@ class Message:
|
||||||
# LookupError will be raised if the charset isn't known to
|
# LookupError will be raised if the charset isn't known to
|
||||||
# Python. UnicodeError will be raised if the encoded text
|
# Python. UnicodeError will be raised if the encoded text
|
||||||
# contains a character not in the charset.
|
# contains a character not in the charset.
|
||||||
charset = str(bytes(charset[2]), pcharset)
|
as_bytes = charset[2].encode('raw-unicode-escape')
|
||||||
|
charset = str(as_bytes, pcharset)
|
||||||
except (LookupError, UnicodeError):
|
except (LookupError, UnicodeError):
|
||||||
charset = charset[2]
|
charset = charset[2]
|
||||||
# charset characters must be in us-ascii range
|
# charset characters must be in us-ascii range
|
||||||
|
|
|
@ -55,7 +55,10 @@ EMPTYSTRING = ''
|
||||||
# See also Charset.py
|
# See also Charset.py
|
||||||
MISC_LEN = 7
|
MISC_LEN = 7
|
||||||
|
|
||||||
HEADER_SAFE_BYTES = b'-!*+/ ' + bytes(ascii_letters) + bytes(digits)
|
HEADER_SAFE_BYTES = (b'-!*+/ ' +
|
||||||
|
ascii_letters.encode('raw-unicode-escape') +
|
||||||
|
digits.encode('raw-unicode-escape'))
|
||||||
|
|
||||||
BODY_SAFE_BYTES = (b' !"#$%&\'()*+,-./0123456789:;<>'
|
BODY_SAFE_BYTES = (b' !"#$%&\'()*+,-./0123456789:;<>'
|
||||||
b'?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`'
|
b'?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`'
|
||||||
b'abcdefghijklmnopqrstuvwxyz{|}~\t')
|
b'abcdefghijklmnopqrstuvwxyz{|}~\t')
|
||||||
|
|
|
@ -2531,10 +2531,8 @@ class TestBase64(unittest.TestCase):
|
||||||
|
|
||||||
def test_decode(self):
|
def test_decode(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
eq(base64mime.decode(''), '')
|
eq(base64mime.decode(''), b'')
|
||||||
eq(base64mime.decode('aGVsbG8='), b'hello')
|
eq(base64mime.decode('aGVsbG8='), b'hello')
|
||||||
eq(base64mime.decode('aGVsbG8=', 'X'), b'hello')
|
|
||||||
eq(base64mime.decode('aGVsbG8NCndvcmxk\n', 'X'), b'helloXworld')
|
|
||||||
|
|
||||||
def test_encode(self):
|
def test_encode(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue