File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ):
Original file line number Diff line number Diff line change @@ -677,6 +677,27 @@ def test_encode7or8bit(self):
677677 msg = MIMEText ('文' , _charset = 'euc-jp' )
678678 eq (msg ['content-transfer-encoding' ], '7bit' )
679679
680+ def test_qp_encode_latin1 (self ):
681+ msg = MIMEText ('\xe1 \xf6 \n ' , 'text' , 'ISO-8859-1' )
682+ self .assertEqual (str (msg ), textwrap .dedent ("""\
683+ MIME-Version: 1.0
684+ Content-Type: text/text; charset="iso-8859-1"
685+ Content-Transfer-Encoding: quoted-printable
686+
687+ =E1=F6
688+ """ ))
689+
690+ def test_qp_encode_non_latin1 (self ):
691+ # Issue 16948
692+ msg = MIMEText ('\u017c \n ' , 'text' , 'ISO-8859-2' )
693+ self .assertEqual (str (msg ), textwrap .dedent ("""\
694+ MIME-Version: 1.0
695+ Content-Type: text/text; charset="iso-8859-2"
696+ Content-Transfer-Encoding: quoted-printable
697+
698+ =BF
699+ """ ))
700+
680701
681702# Test long header wrapping
682703class TestLongHeaders (TestEmailBase ):
Original file line number Diff line number Diff line change @@ -163,6 +163,9 @@ Core and Builtins
163163Library
164164-------
165165
166+ - Issue #16948: Fix quoted printable body encoding for non-latin1 character
167+ sets in the email package.
168+
166169- Issue #16811: Fix folding of headers with no value in the provisional email
167170 policies.
168171
You can’t perform that action at this time.
0 commit comments