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

Skip to content

Commit 775632b

Browse files
committed
#19957: Simplify encode_7or8bit now that _payload is always str.
Patch by Vajrasky Kok, test enhancement by me.
1 parent cd0cb8c commit 775632b

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

Lib/email/encoders.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,12 @@ def encode_7or8bit(msg):
5454
# There's no payload. For backwards compatibility we use 7bit
5555
msg['Content-Transfer-Encoding'] = '7bit'
5656
return
57-
# We play a trick to make this go fast. If encoding/decode to ASCII
58-
# succeeds, we know the data must be 7bit, otherwise treat it as 8bit.
57+
# We play a trick to make this go fast. If decoding from ASCII succeeds,
58+
# we know the data must be 7bit, otherwise treat it as 8bit.
5959
try:
60-
if isinstance(orig, str):
61-
orig.encode('ascii')
62-
else:
63-
orig.decode('ascii')
60+
orig.decode('ascii')
6461
except UnicodeError:
65-
charset = msg.get_charset()
66-
output_cset = charset and charset.output_charset
67-
# iso-2022-* is non-ASCII but encodes to a 7-bit representation
68-
if output_cset and output_cset.lower().startswith('iso-2022-'):
69-
msg['Content-Transfer-Encoding'] = '7bit'
70-
else:
71-
msg['Content-Transfer-Encoding'] = '8bit'
62+
msg['Content-Transfer-Encoding'] = '8bit'
7263
else:
7364
msg['Content-Transfer-Encoding'] = '7bit'
7465

Lib/test/test_email/test_email.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,15 @@ def test_encode7or8bit(self):
765765
# whose output character set is 7bit gets a transfer-encoding
766766
# of 7bit.
767767
eq = self.assertEqual
768-
msg = MIMEText('文', _charset='euc-jp')
768+
msg = MIMEText('文\n', _charset='euc-jp')
769769
eq(msg['content-transfer-encoding'], '7bit')
770+
eq(msg.as_string(), textwrap.dedent("""\
771+
MIME-Version: 1.0
772+
Content-Type: text/plain; charset="iso-2022-jp"
773+
Content-Transfer-Encoding: 7bit
774+
775+
\x1b$BJ8\x1b(B
776+
"""))
770777

771778
def test_qp_encode_latin1(self):
772779
msg = MIMEText('\xe1\xf6\n', 'text', 'ISO-8859-1')

0 commit comments

Comments
 (0)