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.

classification
Title: email.generator.Generator.flatten() fails
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: r.david.murray Nosy List: r.david.murray, sdaoden, terry.reedy
Priority: normal Keywords: patch

Created on 2011-04-06 13:49 by sdaoden, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
11782.1.diff sdaoden, 2011-04-11 09:59 review
Messages (7)
msg133128 - (view) Author: Steffen Daode Nurpmeso (sdaoden) Date: 2011-04-06 13:49
This snippet (for #11684, but it's simply BytesParser with
headersonly=True in the end)


        with openfile('msg_46.txt', 'rb') as fp:
            msgdata = fp.read()
        parser = email.parser.BytesHeaderParser()
        msg = parser.parsebytes(msgdata)
        out = BytesIO()
        gen = email.generator.BytesGenerator(out)
        gen.flatten(msg)
        self.assertEqual(out.getvalue(), msgdata)


causes this error:


ERROR: test_byte_message_rfc822_only (test_email.TestMessageAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/test/test_email/test_email.py", line 200, in test_byte_message_rfc822_only
    gen.flatten(msg)
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/email/generator.py", line 91, in flatten
    self._write(msg)
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/email/generator.py", line 137, in _write
    self._dispatch(msg)
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/email/generator.py", line 163, in _dispatch
    meth(msg)
  File "/Users/steffen/usr/opt/py3k/lib/python3.3/email/generator.py", line 304, in _handle_message
    self._fp.write(payload)
TypeError: 'str' does not support the buffer interface
msg133357 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-04-08 23:39
The error message suggests that somehow a str string has gotten mixed in with the bytes. Something like '\n' or ' '?

Steffen, if you want to help track this down:
1. What is a minimal msgdata that gives the same error; post it.
2. Add 'print(payload)' before line 304: self._fp.write(payload)
and perhaps further back to see what the offending string is and where it comes from.
msg133360 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-04-09 00:29
Terry, the test is in the other issue, so this time Steffen has provided the test :).

I'll take a look at both issues, probably next week.
msg133383 - (view) Author: Steffen Daode Nurpmeso (sdaoden) Date: 2011-04-09 11:56
On Fri, Apr 08, 2011 at 11:39:36PM +0000, Terry J. Reedy wrote:
> 1. What is a minimal msgdata that gives the same error; post it.

Stepping a bit..  Remove 'Content-Type' header field and this does 
not crash.  Thus the real problem may again lie in get_payload() 
trying to convert data instead of leaving it alone. 
I'll try to instrument the path a bit ..

import email, email.parser, email.generator, io, textwrap
msgdata = textwrap.dedent("""\
            Date: Mon, 01 Feb 2010 12:21:16 +0100
            From: "three" <four@example.net>
            To: <one@two.com>
            Subject: Content-Type crash
            Content-Type: message/rfc822

            Remove Content-Type header field and no crash happens!
        """).encode('ascii')
msg = email.parser.BytesParser().parsebytes(msgdata, headersonly=True)
email.generator.BytesGenerator(io.BytesIO()).flatten(msg)
msg133410 - (view) Author: Steffen Daode Nurpmeso (sdaoden) Date: 2011-04-09 18:45
On Sat, Apr 09, 2011 at 11:56:16AM +0000, Steffen Daode Nurpmeso wrote:
> I'll try to instrument the path a bit ..

Sorry, no time today.  All the stuff next week.
Nice weekend.
msg133508 - (view) Author: Steffen Daode Nurpmeso (sdaoden) Date: 2011-04-11 09:59
So here is a patch which steps forward the not-yet-fully-completed 
transition to the mixed bytes/str EMail stuff. 
Test coverage is available if you patch in 
http://bugs.python.org/file21549/11684.1.diff from #11684.
(Because i leak the great picture of this module ... say.)
msg133691 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-04-13 20:48
I applied this as part of #11684.  Thanks.
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55991
2011-04-13 20:48:02r.david.murraysetstatus: open -> closed
type: behavior
messages: + msg133691

resolution: fixed
stage: resolved
2011-04-11 09:59:50sdaodensetfiles: + 11782.1.diff
keywords: + patch
messages: + msg133508
2011-04-09 18:45:34sdaodensetmessages: + msg133410
2011-04-09 11:56:16sdaodensetmessages: + msg133383
2011-04-09 00:29:50r.david.murraysetmessages: + msg133360
2011-04-08 23:39:35terry.reedysetnosy: + terry.reedy
messages: + msg133357
2011-04-06 13:55:13r.david.murraysetassignee: r.david.murray

nosy: + r.david.murray
2011-04-06 13:49:43sdaodencreate