[ python-Bugs-1017329 ] email.Message does not allow iteration

SourceForge.net noreply at sourceforge.net
Mon Nov 1 14:40:57 CET 2004


Bugs item #1017329, was opened at 2004-08-26 22:11
Message generated for change (Comment added) made by bwarsaw
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1017329&group_id=5470

Category: Extension Modules
Group: None
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Paul McGuire (ptmcg)
Assigned to: Barry A. Warsaw (bwarsaw)
Summary: email.Message does not allow iteration

Initial Comment:
>From comp.lang.python:

"Roman Suzi" <rnd at onego.ru> wrote in message
news:mailman.2348.1093434827.5135.python-
list at python.org...
>
> In Python2.3.4:
>
> >>> em = email.message_from_file(open
('MAILMESSAGE'))
> >>> for i in em:
> ...   print i
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.3/email/Message.py",
> line
> 304, in __getitem__
>   File "/usr/lib/python2.3/email/Message.py",
> line
> 370, in get
> AttributeError: 'int' object has no attribute 'lower'

In this example, em is an email.Message object.  The 
Message acts as a pseudo-dictionary, containing a list of 
e-mail fields, organized by name and contents.

The problem occurs because __getitem__ assumes that 
the provided index is a name into a dictionary.  For 
programmer convenience, keys in the Message are case-
insensitive, so __getitem__ starts by calling lower() on 
the provided name.  Apparently, the attempt to iterate 
over Message successively calls __getitem__ with 
integers from 0 to len(em).  Integers don't like having 
lower() called, though.

To fix this problem:
- have __getitem__ use the input arg as a name if it is 
of type string; otherwise, just use the provided arg as 
an index to the underlying self.headers list (arg could 
be either an integer or a slice, so don't just test for 
integer!)
- make a similar change to __delitem__ - it too 
assumes that the incoming arg is a string, naming a 
header field to be deleted; must test first to see if arg is 
an integer or slice
- add __iter__ method, which returns iter(self.headers)

-- Paul

----------------------------------------------------------------------

>Comment By: Barry A. Warsaw (bwarsaw)
Date: 2004-11-01 08:40

Message:
Logged In: YES 
user_id=12800

Header iteration is already supported by .keys(), .values(),
and .items() and it's ambiguous whether "for x in msg"
should return the headers or in the case of multiparts, the
subparts.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1017329&group_id=5470


More information about the Python-bugs-list mailing list