Skip to content

Commit f581b37

Browse files
committed
#16948: Fix quopri encoding of non-latin1 character sets.
1 parent 43536e9 commit f581b37

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Lib/email/charset.py

+13
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ def body_encode(self, string):
392392
string = string.encode(self.output_charset)
393393
return email.base64mime.body_encode(string)
394394
elif self.body_encoding is QP:
395+
# quopromime.body_encode takes a string, but operates on it as if
396+
# it were a list of byte codes. For a (minimal) history on why
397+
# this is so, see changeset 0cf700464177. To correctly encode a
398+
# character set, then, we must turn it into pseudo bytes via the
399+
# latin1 charset, which will encode any byte as a single code point
400+
# between 0 and 255, which is what body_encode is expecting.
401+
#
402+
# Note that this clause doesn't handle the case of a _payload that
403+
# is already bytes. It never did, and the semantics of _payload
404+
# being bytes has never been nailed down, so fixing that is a
405+
# longer term TODO.
406+
if isinstance(string, str):
407+
string = string.encode(self.output_charset).decode('latin1')
395408
return email.quoprimime.body_encode(string)
396409
else:
397410
if isinstance(string, str):

Lib/email/test/test_email.py

+21
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,27 @@ def test_encode7or8bit(self):
670670
msg = MIMEText('文', _charset='euc-jp')
671671
eq(msg['content-transfer-encoding'], '7bit')
672672

673+
def test_qp_encode_latin1(self):
674+
msg = MIMEText('\xe1\xf6\n', 'text', 'ISO-8859-1')
675+
self.assertEqual(str(msg), textwrap.dedent("""\
676+
MIME-Version: 1.0
677+
Content-Type: text/text; charset="iso-8859-1"
678+
Content-Transfer-Encoding: quoted-printable
679+
680+
=E1=F6
681+
"""))
682+
683+
def test_qp_encode_non_latin1(self):
684+
# Issue 16948
685+
msg = MIMEText('\u017c\n', 'text', 'ISO-8859-2')
686+
self.assertEqual(str(msg), textwrap.dedent("""\
687+
MIME-Version: 1.0
688+
Content-Type: text/text; charset="iso-8859-2"
689+
Content-Transfer-Encoding: quoted-printable
690+
691+
=BF
692+
"""))
693+
673694

674695
# Test long header wrapping
675696
class TestLongHeaders(TestEmailBase):

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ Core and Builtins
212212
Library
213213
-------
214214

215+
216+
- Issue #16948: Fix quoted printable body encoding for non-latin1 character
217+
sets in the email package.
218+
215219
- Issue #17089: Expat parser now correctly works with string input not only when
216220
an internal XML encoding is UTF-8 or US-ASCII. It now accepts bytes and
217221
strings larger than 2 GiB.

0 commit comments

Comments
 (0)