[New-bugs-announce] [issue27240] 'UnstructuredTokenList' object has no attribute '_fold_as_ew'

Leblond Emmanuel report at bugs.python.org
Mon Jun 6 06:25:59 EDT 2016

New submission from Leblond Emmanuel:

The folding code for email headers crashes whenever it hits line _header_value_parser.py:432 given it try to call method `_fold_as_ew`.

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

components: email
files: test_mail_2.py
messages: 267525
nosy: Leblond Emmanuel, barry, r.david.murray
priority: normal
severity: normal
status: open
title: 'UnstructuredTokenList' object has no attribute '_fold_as_ew'
versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file43257/test_mail_2.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list