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.Message does not allow iteration
Type: Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, ptmcg
Priority: normal Keywords:

Created on 2004-08-27 02:11 by ptmcg, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg22232 - (view) Author: Paul McGuire (ptmcg) Date: 2004-08-27 02:11
From comp.lang.python:

"Roman Suzi" <rnd@onego.ru> wrote in message
news:mailman.2348.1093434827.5135.python-
list@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
msg22233 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2004-11-01 13:40
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.
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40829
2004-08-27 02:11:01ptmcgcreate