classification
Title: non-ascii characters in headers causes TypeError on email.policy.Policy.fold
Type: behavior Stage: patch review
Components: email Versions: Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: altvod, barry, matrixise, r.david.murray, rad164
Priority: normal Keywords: patch

Created on 2018-05-15 16:35 by rad164, last changed 2019-02-13 12:58 by matrixise.

Pull Requests
URL Status Linked Edit
PR 6966 open licht-t, 2018-05-18 12:03
Messages (3)
msg316680 - (view) Author: Rad164 (rad164) Date: 2018-05-15 16:35
Python 3.6.5 has trouble on folding email messages:

Traceback (most recent call last):
  File "emailtest.py", line 7, in <module>
    policy.fold("Subject", msg["Subject"])
  File "/usr/lib/python3.6/email/policy.py", line 183, in fold
    return self._fold(name, value, refold_binary=True)
  File "/usr/lib/python3.6/email/policy.py", line 205, in _fold
    return value.fold(policy=self)
  File "/usr/lib/python3.6/email/headerregistry.py", line 258, in fold
    return header.fold(policy=policy)
  File "/usr/lib/python3.6/email/_header_value_parser.py", line 144, in fold
    return _refold_parse_tree(self, policy=policy)
  File "/usr/lib/python3.6/email/_header_value_parser.py", line 2651, in _refold_parse_tree
    part.ew_combine_allowed, charset)
  File "/usr/lib/python3.6/email/_header_value_parser.py", line 2728, in _fold_as_ew
    first_part = to_encode[:text_space]
TypeError: slice indices must be integers or None or have an __index__ method


The message has non-ascii characters in header and set max_line_length=0, regardless length of the header. Here is the code to reproduce.


from email.message import EmailMessage
from email.policy import default

policy = default.clone(max_line_length=0)
msg = EmailMessage()
msg["Subject"] = "á"
policy.fold("Subject", msg["Subject"])


I first found this issue on Maildir.add, which saves the message to a file without word wrap.
msg316681 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-15 17:04
Oh, interesting.

I could argue that that's a missing feature in Python's slice handling.  The value of text_space at that point is '+inf', and I obviously incorrectly assumed that slicing would treat that as if it were [:].  The counter argument, of course, is that inf is a float not an integer.  I suppose we'll have to use sys.maxsize instead.
msg335436 - (view) Author: Stéphane Wirtel (matrixise) * (Python triager) Date: 2019-02-13 12:58
See this issue as a duplicata of this one:

https://bugs.python.org/issue35985
History
Date User Action Args
2019-02-13 13:06:27xtreaklinkissue35985 superseder
2019-02-13 12:58:43matrixisesetnosy: + matrixise
messages: + msg335436
2018-07-25 14:51:41r.david.murraysetnosy: + altvod

versions: + Python 3.7, Python 3.8, - Python 3.6
2018-07-25 14:51:04r.david.murraylinkissue34220 superseder
2018-05-18 12:03:03licht-tsetkeywords: + patch
stage: patch review
pull_requests: + pull_request6625
2018-05-15 17:04:08r.david.murraysetmessages: + msg316681
2018-05-15 16:35:30rad164create