email.message.Message - as_string fails

Terry Reedy tjreedy at
Sat Dec 29 02:57:46 CET 2012

On 12/28/2012 7:22 AM, Helmut Jarausch wrote:
> Hi,
> I'm trying to filter an mbox file by removing some messages.
> For that I use
> Parser= FeedParser(policy=policy.SMTP)
> and 'feed' any lines to it.
> If the mbox file contains a white line followed by '^From ',
> I do
> Msg= Parser.close()
> (lateron I delete the Parser and create a new one by
> Parser= FeedParser(policy=policy.SMTP)
> )
> I can access parts of the message by  Msg['Message-ID'], e.g.
> but even for the very first message, trying to print it or convert it to a string
> by  MsgStr=Msg.as_string(unixfrom=True)
> lets Python (3.3.1_pre20121209) die with
> Traceback (most recent call last):
>    File "", line 35, in <module>
>      MsgStr=Msg.as_string(unixfrom=True)
>    File "/usr/lib64/python3.3/email/", line 151, in as_string
>      g.flatten(self, unixfrom=unixfrom)
>    File "/usr/lib64/python3.3/email/", line 112, in flatten
>      self._write(msg)
>    File "/usr/lib64/python3.3/email/", line 171, in _write
>      self._write_headers(msg)
>    File "/usr/lib64/python3.3/email/", line 198, in _write_headers
>      self.write(self.policy.fold(h, v))
>    File "/usr/lib64/python3.3/email/", line 153, in fold
>      return self._fold(name, value, refold_binary=True)
>    File "/usr/lib64/python3.3/email/", line 176, in _fold
>      (len(lines[0])+len(name)+2 > maxlen or
> IndexError: list index out of range

The only list index visible is 0 in lines[0]. If this raises, lines is 
empty. You could trace back to see where lines is defined. I suspect it 
is all or part of the Msg you started with.

I believe that some of email was rewritten for 3.3, so it is possible 
that you found a bug based on an untrue assumption. It is also possible 
that you missed a limitation in the doc, or tripped over an intended but 
not written limitation. So I hope you do the tracing, so if doc or code 
need a fix, a tracker issue can be opened.

Terry Jan Reedy

More information about the Python-list mailing list