Issue 16811: email.message.Message flatten dies of list index out of range (original) (raw)

The following code triggers the bug:

#!/usr/bin/python3.3 #-- coding: latin1 -- from email.message import Message from email import policy from email.parser import FeedParser

Parser= FeedParser(policy=policy.SMTP) Parser.feed('From jarausch@igpm.rwth-aachen.de Tue Apr 24 15:09:24 2012\n') Parser.feed('X-Status: \n') # this triggers the bug Parser.feed('From: Helmut Jarausch <jarausch@igpm.rwth-aachen.de>\n')

Msg= Parser.close() Msg_as_str= Msg.as_string(unixfrom=True)

This triggers File "Email_Parse_Bug.py", line 13, in Msg_as_str= Msg.as_string(unixfrom=True) File "/usr/lib64/python3.3/email/message.py", line 151, in as_string g.flatten(self, unixfrom=unixfrom) File "/usr/lib64/python3.3/email/generator.py", line 112, in flatten self._write(msg) File "/usr/lib64/python3.3/email/generator.py", line 171, in _write self._write_headers(msg) File "/usr/lib64/python3.3/email/generator.py", line 198, in _write_headers self.write(self.policy.fold(h, v)) File "/usr/lib64/python3.3/email/policy.py", line 153, in fold return self._fold(name, value, refold_binary=True) File "/usr/lib64/python3.3/email/policy.py", line 176, in _fold (len(lines[0])+len(name)+2 > maxlen or IndexError: list index out of range

If I strip the '\n' from the lines feeded to the Parser, the bug does not occur.

Thanks for looking into it, Helmut.

This is with Python 3.3.0+ 3.3:ccc372b37fbb+