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

Skip to content

Commit 2cc1f6d

Browse files
committed
More email package related repairs. This fixes smtplib's import and use of
email.base64mime, but test_smtplib still has failures for me. They are timeout errors so think they're more related to my current wacky network setup than bugs remaining in the code related to the email package. This also r57693 that got clobbered with the sandbox sync, and fixes a couple of other minor problems that cropped up. I will kill the sandbox branch next. The email package now has 11F/11E.
1 parent 5a23cc5 commit 2cc1f6d

6 files changed

Lines changed: 23 additions & 28 deletions

File tree

Lib/email/base64mime.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,19 @@ def body_encode(s, maxlinelen=76, eol=NL):
101101

102102

103103

104-
def decode(s, convert_eols=False):
104+
def decode(string):
105105
"""Decode a raw base64 string, returning a bytes object.
106106
107-
If convert_eols is set to a string value, all canonical email linefeeds,
108-
e.g. "\\r\\n", in the decoded text will be converted to the value of
109-
convert_eols. os.linesep is a good choice for convert_eols if you are
110-
decoding a text attachment.
111-
112107
This function does not parse a full MIME header value encoded with
113108
base64 (like =?iso-8895-1?b?bmloISBuaWgh?=) -- please use the high
114109
level email.Header class for that functionality.
115110
"""
116-
if not s:
117-
return s
118-
119-
dec = a2b_base64(s)
120-
if convert_eols:
121-
return dec.replace(CRLF, convert_eols)
122-
return dec
111+
if not string:
112+
return bytes()
113+
elif isinstance(string, str):
114+
return a2b_base64(string.encode('raw-unicode-escape'))
115+
else:
116+
return a2b_base64(s)
123117

124118

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

Lib/email/header.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,11 @@ def __init__(self, headerlen, maxlen, continuation_ws, splitchars):
341341
self._current_line = _Accumulator(headerlen)
342342

343343
def __str__(self):
344-
# Remove the trailing TRANSITIONAL_SPACE
345-
last_line = self._current_line.pop()
346-
if last_line is not TRANSITIONAL_SPACE:
347-
self._current_line.push(last_line)
344+
# Remove any trailing TRANSITIONAL_SPACE
345+
if len(self._current_line) > 0:
346+
last_line = self._current_line.pop()
347+
if last_line is not TRANSITIONAL_SPACE:
348+
self._current_line.push(last_line)
348349
self.newline()
349350
return NL.join(self._lines)
350351

Lib/email/message.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ 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+
payload += '\n'
205+
in_file = BytesIO(payload.encode('raw-unicode-escape'))
205206
out_file = BytesIO()
206207
try:
207208
uu.decode(in_file, out_file, quiet=True)
@@ -752,7 +753,8 @@ def get_content_charset(self, failobj=None):
752753
# LookupError will be raised if the charset isn't known to
753754
# Python. UnicodeError will be raised if the encoded text
754755
# contains a character not in the charset.
755-
charset = str(bytes(charset[2]), pcharset)
756+
as_bytes = charset[2].encode('raw-unicode-escape')
757+
charset = str(as_bytes, pcharset)
756758
except (LookupError, UnicodeError):
757759
charset = charset[2]
758760
# charset characters must be in us-ascii range

Lib/email/quoprimime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
_QUOPRI_BODY_MAP = _QUOPRI_HEADER_MAP.copy()
5959

6060
# Safe header bytes which need no encoding.
61-
for c in b'-!*+/' + bytes(ascii_letters, 'ascii') + bytes(digits, 'ascii'):
61+
for c in b'-!*+/' + ascii_letters.encode('ascii') + digits.encode('ascii'):
6262
_QUOPRI_HEADER_MAP[c] = chr(c)
6363
# Headers have one other special encoding; spaces become underscores.
6464
_QUOPRI_HEADER_MAP[ord(' ')] = '_'

Lib/email/test/test_email.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,10 +2538,8 @@ def test_len(self):
25382538

25392539
def test_decode(self):
25402540
eq = self.assertEqual
2541-
eq(base64mime.decode(''), '')
2541+
eq(base64mime.decode(''), b'')
25422542
eq(base64mime.decode('aGVsbG8='), b'hello')
2543-
eq(base64mime.decode('aGVsbG8=', 'X'), b'hello')
2544-
eq(base64mime.decode('aGVsbG8NCndvcmxk\n', 'X'), b'helloXworld')
25452543

25462544
def test_encode(self):
25472545
eq = self.assertEqual

Lib/smtplib.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import email.utils
4747
import base64
4848
import hmac
49-
from email.base64mime import encode as encode_base64
49+
from email.base64mime import body_encode as encode_base64
5050
from sys import stderr
5151

5252
__all__ = ["SMTPException","SMTPServerDisconnected","SMTPResponseException",
@@ -529,10 +529,10 @@ def login(self, user, password):
529529
def encode_cram_md5(challenge, user, password):
530530
challenge = base64.decodestring(challenge)
531531
response = user + " " + hmac.HMAC(password, challenge).hexdigest()
532-
return encode_base64(response, eol="")
532+
return encode_base64(response)
533533

534534
def encode_plain(user, password):
535-
return encode_base64("\0%s\0%s" % (user, password), eol="")
535+
return encode_base64("\0%s\0%s" % (user, password))
536536

537537

538538
AUTH_PLAIN = "PLAIN"
@@ -574,10 +574,10 @@ def encode_plain(user, password):
574574
AUTH_PLAIN + " " + encode_plain(user, password))
575575
elif authmethod == AUTH_LOGIN:
576576
(code, resp) = self.docmd("AUTH",
577-
"%s %s" % (AUTH_LOGIN, encode_base64(user, eol="")))
577+
"%s %s" % (AUTH_LOGIN, encode_base64(user)))
578578
if code != 334:
579579
raise SMTPAuthenticationError(code, resp)
580-
(code, resp) = self.docmd(encode_base64(password, eol=""))
580+
(code, resp) = self.docmd(encode_base64(password))
581581
elif authmethod is None:
582582
raise SMTPException("No suitable authentication method found.")
583583
if code not in (235, 503):

0 commit comments

Comments
 (0)