Bug in email package?

R

Roman Suzi

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

....
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
 
E

Erik Max Francis

Roman said:
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

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

Steven Bethard

Erik said:
Roman said:
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

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

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
 
T

Tim Roberts

Roman Suzi said:
I was playing with email package and discovrered this strange kind of
behaviour:
import email.Message
m = email.Message.Message()
m['a'] = '123'
print m
From nobody Mon Feb 21 00:12:27 2005
a: 123
...
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'

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

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.
Can this behaviour of email be considered a bug?

Not in my opinion, no.
Is there a good case to iterate over something useful in a message?

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

Again, I'm not sure what, intuitively, it would mean to iterate over an
rfc822 object.
 
R

Roman Suzi

Erik said:
Roman said:
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

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

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

Well, that was what I meant.
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.

Ok. A bug minute on the next bug day ;-)
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

Sincerely yours, Roman Suzi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top