This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.quoprimime.body_encode can't handle characters that encodings.quopri_codec can
Type: behavior Stage: resolved
Components: email Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: email.mime.text.MIMEText: QP encoding broken with charset!=ISO-8859-1
View: 16948
Assigned To: Nosy List: barry, r.david.murray, rpatterson
Priority: normal Keywords:

Created on 2013-03-20 18:15 by rpatterson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg184772 - (view) Author: Ross Patterson (rpatterson) Date: 2013-03-20 18:15
When using email.charset.Charset to encode MIME bodie as quoted-printable, some characters that are encodable with the quopri_codec cause a KeyError in email.quoprimime:

Python 3.3.0 (default, Oct  7 2012, 14:43:21) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> text_encoded = b'mo \xe2\x82\xac'
>>> text = text_encoded.decode('utf-8')
>>> print(text)
mo €
>>> import codecs
>>> codecs.getencoder('quopri_codec')(text_encoded)[0]
b'mo=20=E2=82=AC'
>>> from email import charset
>>> c = charset.Charset('utf-8')
>>> c.header_encoding = charset.QP
>>> c.body_encoding = charset.QP
>>> c.header_encode(text)
'=?utf-8?q?mo_=E2=82=AC?='
>>> c.body_encode(text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/email/charset.py", line 395, in body_encode
    return email.quoprimime.body_encode(string)
  File "/usr/lib/python3.3/email/quoprimime.py", line 240, in body_encode
    if body_check(ord(c)):
  File "/usr/lib/python3.3/email/quoprimime.py", line 81, in body_check
    return chr(octet) != _QUOPRI_BODY_MAP[octet]
KeyError: 8364
msg184774 - (view) Author: Ross Patterson (rpatterson) Date: 2013-03-20 18:16
What is the reason that email.quoprimime doesn't use codecs.getencoder('quopri_codec') to do the actual character encoding?
msg184777 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-20 19:05
There is a long history behind quoprime in the email package, and I don't know all of it.  It is on my list of things to sort out (there is also potential duplication with the binascii module), but I haven't gotten to it yet.

The issue you report, though is a duplicate of issue 16948 and is already fixed in the repo.

You checked off 2.7.  Did you actually see a failure in 2.7?  It shouldn't be broken there.
msg184778 - (view) Author: Ross Patterson (rpatterson) Date: 2013-03-20 19:07
I thought I had tested it under 2.7, but I'm not entirely sure.
msg184779 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-20 19:12
OK, if you find it is a bug on 2.7 you can reopen.
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61697
2013-03-20 19:12:55r.david.murraysetsuperseder: email.mime.text.MIMEText: QP encoding broken with charset!=ISO-8859-1
messages: + msg184779
stage: resolved
2013-03-20 19:08:50rpattersonsetstatus: open -> closed
resolution: duplicate
2013-03-20 19:07:42rpattersonsetmessages: + msg184778
versions: - Python 2.6, Python 3.1, Python 2.7, Python 3.2
2013-03-20 19:05:43r.david.murraysetmessages: + msg184777
2013-03-20 18:16:39rpattersonsetmessages: + msg184774
2013-03-20 18:15:36rpattersoncreate