email module Bug in 2.2

Quinn Dunkan quinn at vomit.ugcs.caltech.edu
Sat Feb 9 00:58:53 EST 2002


On Fri, 08 Feb 2002 18:41:14 -0800, Sheila King <usenet at thinkspot.net> wrote:
>OK, here is a bug in the mail module. It has to be????
>
>I took an email that I received (a spam) and saved it to a text 
>file (which I called 'dtv.txt') and then imported it into an email class
>object in an interactive session. 
>
>I've pasted below an interactive session, and below that I've pasted the text 
>file that was used in the session. There seem to be some inconsistencies?

...

>E:\Web\Thinkspotwebsite\dev>python
>Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
>Type "help", "copyright", "credits" or "license" for more information.
>>>> import email
>>>> msg = email.message_from_file(open('dtv.txt'))
>>>> msg.get_type()
>'multipart/related'
>>>> msg.get_main_type()
>'multipart'
>>>> msg.is_multipart()
>0
>>>>

>From the libref:
is_multipart()
Return 1 if the message's payload is a list of sub-Message objects, otherwise
return 0. When is_multipart() returns 0, the payload should either be a string
object, or a single Message instance.

So notice that is_multipart() does not check if get_main_type() is 'multipart'.
I don't know much about the MIME hairball, but the msg appears to claim it is
multipart, but doesn't actually have multiple parts.  Message.add_payload only
sets the payload to a list if it's called more than once, not if the msg calls
itself 'multipart'.  I'm not sure if this is correct or not, though I'm
inclined to agree with you that it's not (section 12.2.2.2 implies that you
should only get a non-string, non-list payload for some message/* types), but
is_multipart() is behaving strictly according to documentation.

As far as practicality goes, when recursing over MIME objects, you're going to
have to handle the non-multipart non-string case anyway, because of those
message/* types.  My MIME code looks like:

def frob_mime(msg):
    if msg.is_multipart():
        for m in msg.get_payload():
            frob_mime(m)
    elif hasattr(msg.get_payload(), 'get_payload'):
        from_mime(msg.get_payload())
    else:
        t = msg.get_type()
        ... do stuff



More information about the Python-list mailing list