Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b3d659

Browse files
committed
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.
1 parent ce36ad8 commit 8b3d659

4 files changed

Lines changed: 20 additions & 24 deletions

File tree

Lib/email/base64mime.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=False,
109109
lines = []
110110
for line in base64ed:
111111
# Ignore the last character of each line if it is a newline
112-
if line.endswith(NL):
112+
if line[-1] == ord(NL):
113113
line = line[:-1]
114114
# Add the chrome
115115
lines.append('=?%s?b?%s?=' % (charset, line))
@@ -158,25 +158,19 @@ def encode(s, binary=True, maxlinelen=76, eol=NL):
158158

159159

160160

161-
def decode(s, convert_eols=False):
161+
def decode(string):
162162
"""Decode a raw base64 string, returning a bytes object.
163163
164-
If convert_eols is set to a string value, all canonical email linefeeds,
165-
e.g. "\\r\\n", in the decoded text will be converted to the value of
166-
convert_eols. os.linesep is a good choice for convert_eols if you are
167-
decoding a text attachment.
168-
169-
This function does not parse a full MIME header value encoded with
170-
base64 (like =?iso-8895-1?b?bmloISBuaWgh?=) -- please use the high
171-
level email.Header class for that functionality.
164+
This function does not parse a full MIME header value encoded with base64
165+
(like =?iso-8895-1?b?bmloISBuaWgh?=) -- use the high level
166+
email.Header class for that functionality.
172167
"""
173-
if not s:
174-
return s
175-
176-
dec = a2b_base64(s)
177-
if convert_eols:
178-
return dec.replace(CRLF, convert_eols)
179-
return dec
168+
if not string:
169+
return bytes()
170+
elif isinstance(string, str):
171+
return a2b_base64(string.encode('raw-unicode-escape'))
172+
else:
173+
return a2b_base64(string)
180174

181175

182176
# For convenience and backwards compatibility w/ standard base64 module

Lib/email/message.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def get_payload(self, i=None, decode=False):
201201
# Incorrect padding
202202
pass
203203
elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
204-
in_file = BytesIO(bytes(payload + '\n'))
204+
in_file = BytesIO((payload + '\n').encode('raw-unicode-escape'))
205205
out_file = BytesIO()
206206
try:
207207
uu.decode(in_file, out_file, quiet=True)
@@ -757,7 +757,8 @@ def get_content_charset(self, failobj=None):
757757
# LookupError will be raised if the charset isn't known to
758758
# Python. UnicodeError will be raised if the encoded text
759759
# contains a character not in the charset.
760-
charset = str(bytes(charset[2]), pcharset)
760+
as_bytes = charset[2].encode('raw-unicode-escape')
761+
charset = str(as_bytes, pcharset)
761762
except (LookupError, UnicodeError):
762763
charset = charset[2]
763764
# charset characters must be in us-ascii range

Lib/email/quoprimime.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@
5555
# See also Charset.py
5656
MISC_LEN = 7
5757

58-
HEADER_SAFE_BYTES = b'-!*+/ ' + bytes(ascii_letters) + bytes(digits)
58+
HEADER_SAFE_BYTES = (b'-!*+/ ' +
59+
ascii_letters.encode('raw-unicode-escape') +
60+
digits.encode('raw-unicode-escape'))
61+
5962
BODY_SAFE_BYTES = (b' !"#$%&\'()*+,-./0123456789:;<>'
6063
b'?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`'
6164
b'abcdefghijklmnopqrstuvwxyz{|}~\t')

Lib/email/test/test_email.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,10 +2531,8 @@ def test_len(self):
25312531

25322532
def test_decode(self):
25332533
eq = self.assertEqual
2534-
eq(base64mime.decode(''), '')
2534+
eq(base64mime.decode(''), b'')
25352535
eq(base64mime.decode('aGVsbG8='), b'hello')
2536-
eq(base64mime.decode('aGVsbG8=', 'X'), b'hello')
2537-
eq(base64mime.decode('aGVsbG8NCndvcmxk\n', 'X'), b'helloXworld')
25382536

25392537
def test_encode(self):
25402538
eq = self.assertEqual
@@ -2844,7 +2842,7 @@ def test_multilingual(self):
28442842
def test_empty_header_encode(self):
28452843
h = Header()
28462844
self.assertEqual(h.encode(), '')
2847-
2845+
28482846
def test_header_ctor_default_args(self):
28492847
eq = self.ndiffAssertEqual
28502848
h = Header()

0 commit comments

Comments
 (0)