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.

Author touilleMan
Recipients barry, r.david.murray, touilleMan
Date 2016-06-06.10:25:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The folding code for email headers crashes whenever it hits line given it try to call method `_fold_as_ew`.

(venv)➜  vigiechiro-api git:(in2p3) ✗ python  
Traceback (most recent call last):
  File "", line 36, in <module>
  File "/home/emmanuel/projects/vigiechiro-api/venv/lib/python3.5/site-packages/", line 492, in send
  File "/home/emmanuel/projects/vigiechiro-api/venv/lib/python3.5/site-packages/", line 427, in send
  File "/home/emmanuel/projects/vigiechiro-api/venv/lib/python3.5/site-packages/", line 190, in send
    message.as_bytes() if PY3 else message.as_string(),
  File "/home/emmanuel/projects/vigiechiro-api/venv/lib/python3.5/site-packages/", line 385, in as_bytes
    return self._message().as_bytes()
  File "/usr/lib/python3.5/email/", line 179, in as_bytes
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python3.5/email/", line 115, in flatten
  File "/usr/lib/python3.5/email/", line 195, in _write
  File "/usr/lib/python3.5/email/", line 422, in _write_headers
    self._fp.write(self.policy.fold_binary(h, v))
  File "/usr/lib/python3.5/email/", line 190, in fold_binary
    folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
  File "/usr/lib/python3.5/email/", line 204, in _fold
    return self.header_factory(name, ''.join(lines)).fold(policy=self)
  File "/usr/lib/python3.5/email/", line 255, in fold
    return header.fold(policy=policy)
  File "/usr/lib/python3.5/email/", line 300, in fold
  File "/usr/lib/python3.5/email/", line 1228, in _fold
  File "/usr/lib/python3.5/email/", line 432, in _fold
AttributeError: 'UnstructuredTokenList' object has no attribute '_fold_as_ew'
> /usr/lib/python3.5/email/
-> part._fold_as_ew(folded)

Given folding depends a lot on the processed value, this bug only trigger in very specific encoded headers (I've try to reproduce this bug with various values but only the one which originally made me find the bug worked so far).

I've isolated the bug into a simple test case (see enclosed file).

It seems this part of the code has been rewritten between Python2 and 3 (the file doesn't exists in Python2). So unsurprisingly the bug doesn't trigger in Python2.

Replacing `part._fold_as_ew(folded)` by `part._fold(folded)` seems to fix the bug, but given my poor knowledge of this code, this fix probably produce unwanted behaviors.
Date User Action Args
2016-06-06 10:25:59touilleMansetrecipients: + touilleMan, barry, r.david.murray
2016-06-06 10:25:59touilleMansetmessageid: <>
2016-06-06 10:25:59touilleManlinkissue27240 messages
2016-06-06 10:25:58touilleMancreate