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: Minor refactor of Lib/email/encoders.py
Type: enhancement Stage: resolved
Components: email Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, python-dev, r.david.murray, serhiy.storchaka, vajrasky
Priority: normal Keywords: patch

Created on 2013-12-12 06:21 by vajrasky, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
minor_refactor_encoders_in_email_lib.patch vajrasky, 2013-12-12 06:21 review
minor_refactor_encoders_in_email_lib_v2.patch vajrasky, 2013-12-13 02:09 review
Messages (7)
msg205944 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-12 06:21
In Lib/email/encoders.py:

def encode_7or8bit(msg):
    """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
    orig = msg.get_payload(decode=True)
    if orig is None:
        # There's no payload.  For backwards compatibility we use 7bit
        msg['Content-Transfer-Encoding'] = '7bit'
        return
    # We play a trick to make this go fast.  If encoding/decode to ASCII
    # succeeds, we know the data must be 7bit, otherwise treat it as 8bit.
    try:
        if isinstance(orig, str):
            orig.encode('ascii')
        else:
            orig.decode('ascii')
    except UnicodeError:
        charset = msg.get_charset()


msg.get_payload(decode=True) always return bytes so there is no point of these lines:

        if isinstance(orig, str):
            orig.encode('ascii')
        else:
            orig.decode('ascii')

Attached the patch to refactor this function.
msg205956 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-12 15:24
Good point, thanks for the patch.
msg205960 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-12 16:46
Then special case for iso-2022-* is not needed too.
msg205971 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-12 18:33
Yes, in theory that should be true at this point.
msg206013 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-12-13 02:09
Okay, here is the patch on which we removed checking of special case for iso-2022-*.
msg206018 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-12-13 02:41
New changeset 29e5dd1f608a by R David Murray in branch 'default':
#19957: Simplify encode_7or8bit now that _payload is always str.
http://hg.python.org/cpython/rev/29e5dd1f608a
msg206019 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-13 02:42
Thanks, Vajrasky and Serhiy.
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64156
2013-12-13 02:42:41r.david.murraysetstatus: open -> closed
type: enhancement
messages: + msg206019

resolution: fixed
stage: resolved
2013-12-13 02:41:18python-devsetnosy: + python-dev
messages: + msg206018
2013-12-13 02:09:58vajraskysetfiles: + minor_refactor_encoders_in_email_lib_v2.patch

messages: + msg206013
2013-12-12 18:33:23r.david.murraysetmessages: + msg205971
2013-12-12 16:46:31serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg205960
2013-12-12 15:24:55r.david.murraysetmessages: + msg205956
2013-12-12 06:21:19vajraskycreate