Connecting Tech Pros Worldwide Help | Site Map

Bug in email package?

Roman Suzi
Guest
 
Posts: n/a
#1: Jul 18 '05

I was playing with email package and discovrered this strange kind of
behaviour:
[color=blue][color=green][color=darkred]
>>> import email.Message
>>> m = email.Message.Message()
>>> m['a'] = '123'
>>> print m[/color][/color]
>From nobody Mon Feb 21 00:12:27 2005[/color]
a: 123

[color=blue][color=green][color=darkred]
>>> for i in m: print i[/color][/color][/color]
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.3/email/Message.py", line 304, in __getitem__
return self.get(name)
File "/usr/local/lib/python2.3/email/Message.py", line 370, in get
name = name.lower()
AttributeError: 'int' object has no attribute 'lower'


I think that if any object (from standard library at least) doesn't support
iteration, it should clearly state so.

My guess is that 'for' causes the use of 'm[0]', which is (rightfully) an
error...

Can this behaviour of email be considered a bug?
Is there a good case to iterate over something useful in a message?

P.S. rfc822 has the same behaviour, at least on Python 2.3


Sincerely yours, Roman Suzi
--
rnd@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
Erik Max Francis
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Bug in email package?


Roman Suzi wrote:
[color=blue]
> I think that if any object (from standard library at least) doesn't support
> iteration, it should clearly state so.
>
> My guess is that 'for' causes the use of 'm[0]', which is (rightfully) an
> error...
>
> Can this behaviour of email be considered a bug?
> Is there a good case to iterate over something useful in a message[/color]

Why would it be a bug if the documentation never stated that the object
was iterable?

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Wyrd has swept all my kin / all the brave chiefs away! / Now I must
follow them! -- Beowulf
Steven Bethard
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Bug in email package?


Erik Max Francis wrote:[color=blue]
> Roman Suzi wrote:
>[color=green]
>> I think that if any object (from standard library at least) doesn't
>> support
>> iteration, it should clearly state so.
>>
>> My guess is that 'for' causes the use of 'm[0]', which is (rightfully)
>> an error...
>>
>> Can this behaviour of email be considered a bug?
>> Is there a good case to iterate over something useful in a message[/color]
>
> Why would it be a bug if the documentation never stated that the object
> was iterable?[/color]

I think the bug is not that an error is produced, but that the _wrong_
error is produced. Trying to iterate over something that is not
iterable should produce a TypeError saying so (not an Attribute error):

py> class C(object):
.... pass
....
py> iter(C())
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: iteration over non-sequence

I've actually seen something like this come up before (I think with
email.Message even...) I say call it a bug and submit a patch. It's
pretty easy to fix -- just add an __iter__ method to Message that raises
a TypeError. That makes it clear that Message doesn't intend to support
the getitem protocol -- it just does so accidentally because it provides
__getitem__.

STeVe
Tim Roberts
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Bug in email package?


Roman Suzi <rnd@onego.ru> wrote:[color=blue]
>
>I was playing with email package and discovrered this strange kind of
>behaviour:
>[color=green][color=darkred]
>>>> import email.Message
>>>> m = email.Message.Message()
>>>> m['a'] = '123'
>>>> print m[/color]
>>From nobody Mon Feb 21 00:12:27 2005[/color]
>a: 123
>[color=green][color=darkred]
>>>> for i in m: print i[/color][/color]
>...
>Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "/usr/local/lib/python2.3/email/Message.py", line 304, in __getitem__
> return self.get(name)
> File "/usr/local/lib/python2.3/email/Message.py", line 370, in get
> name = name.lower()
>AttributeError: 'int' object has no attribute 'lower'[/color]

Intuitively, what did you expect this to do? I don't see why a single
message should be iterable.
[color=blue]
>I think that if any object (from standard library at least) doesn't support
>iteration, it should clearly state so.[/color]

That's going a bit far. Iteration is a relatively new addition to Python.
Those classes that DO support iteration generally say so. If it isn't
mentioned, you probaby shouldn't assume it.
[color=blue]
>Can this behaviour of email be considered a bug?[/color]

Not in my opinion, no.
[color=blue]
>Is there a good case to iterate over something useful in a message?[/color]

Well, if you don't have an answer to that question, then why would you
expect it to support iteration?
[color=blue]
>P.S. rfc822 has the same behaviour, at least on Python 2.3[/color]

Again, I'm not sure what, intuitively, it would mean to iterate over an
rfc822 object.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Roman Suzi
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Bug in email package?


On Sun, 20 Feb 2005, Steven Bethard wrote:
[color=blue]
> Erik Max Francis wrote:[color=green]
>> Roman Suzi wrote:
>>[color=darkred]
>>> I think that if any object (from standard library at least) doesn't support
>>> iteration, it should clearly state so.
>>>
>>> My guess is that 'for' causes the use of 'm[0]', which is (rightfully) an
>>> error...
>>>
>>> Can this behaviour of email be considered a bug?
>>> Is there a good case to iterate over something useful in a message[/color]
>>
>> Why would it be a bug if the documentation never stated that the object was
>> iterable?[/color]
>
> I think the bug is not that an error is produced, but that the _wrong_ error
> is produced. Trying to iterate over something that is not iterable should[/color]

Well, that was what I meant.
[color=blue]
> produce a TypeError saying so (not an Attribute error):
>
> py> class C(object):
> ... pass
> ...
> py> iter(C())
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: iteration over non-sequence
>
> I've actually seen something like this come up before (I think with
> email.Message even...) I say call it a bug and submit a patch.[/color]

Ok. A bug minute on the next bug day ;-)
[color=blue]
> It's pretty
> easy to fix -- just add an __iter__ method to Message that raises a TypeError.
> That makes it clear that Message doesn't intend to support the getitem
> protocol -- it just does so accidentally because it provides __getitem__.
>
> STeVe
>[/color]

Sincerely yours, Roman Suzi
--
rnd@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
Closed Thread