File tree 3 files changed +38
-0
lines changed
3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -392,6 +392,19 @@ def body_encode(self, string):
392
392
string = string .encode (self .output_charset )
393
393
return email .base64mime .body_encode (string )
394
394
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' )
395
408
return email .quoprimime .body_encode (string )
396
409
else :
397
410
if isinstance (string , str ):
Original file line number Diff line number Diff line change @@ -670,6 +670,27 @@ def test_encode7or8bit(self):
670
670
msg = MIMEText ('文' , _charset = 'euc-jp' )
671
671
eq (msg ['content-transfer-encoding' ], '7bit' )
672
672
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
+
673
694
674
695
# Test long header wrapping
675
696
class TestLongHeaders (TestEmailBase ):
Original file line number Diff line number Diff line change @@ -212,6 +212,10 @@ Core and Builtins
212
212
Library
213
213
-------
214
214
215
+
216
+ - Issue #16948: Fix quoted printable body encoding for non-latin1 character
217
+ sets in the email package.
218
+
215
219
- Issue #17089: Expat parser now correctly works with string input not only when
216
220
an internal XML encoding is UTF-8 or US-ASCII. It now accepts bytes and
217
221
strings larger than 2 GiB.
You can’t perform that action at this time.
0 commit comments