msg282454 - (view) |
Author: Vitold S (Vitold S) |
Date: 2016-12-05 19:04 |
I have MIME message and parse content. Later I walk on message headers by call for and receive follow traceback: Traceback (most recent call last): ... for m in msg: (msg is email.message.Message instance) File "C:\Python27\lib\email\message.py", line 294, in __getitem__ return self.get(name) File "C:\Python27\lib\email\message.py", line 360, in get name = name.lower() AttributeError: 'int' object has no attribute 'lower' But with items() all work properly. |
|
|
msg282455 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2016-12-05 19:25 |
It looks like your 'name' variable holds an integer, not a string giving a header name. |
|
|
msg282456 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2016-12-05 19:27 |
Oh, I think I misread your traceback. Can you provide the actual code you are running, please? And probably the message you are parsing, as well. |
|
|
msg282465 - (view) |
Author: Martin Panter (martin.panter) *  |
Date: 2016-12-05 21:13 |
You just need a messsage object with one or more header fields: >>> msg = message_from_string("Name: value\r\n\r\n") >>> for m in msg: ... pass ... AttributeError: 'int' object has no attribute 'lower' Python 2 does not implement __iter__(), so it is the default sequence iteration protocol causing this behaviour. The documentation does not mention iteration either: <https://docs.python.org/2.7/library/email.message.html#email.message.Message.__len__>. It mentions a “mapping-like interface”, but since it explicitly lists __len__(), __contains__(), etc, and not __iter__(), I would say you should not assume iteration is supported. Python 3 does implement Message.__iter__(), but it does not seem to be documented. The method seems to be added as a side effect of Guido removing and restoring the email package: https://github.com/python/cpython/commit/1058618#diff-92a78c52ebc0a8908cbd06c8155f8bdb (removed without __iter__) https://hg.python.org/cpython/file/d5f3c2f416f2/Lib/email/message.py (added back including __iter__) |
|
|
msg282469 - (view) |
Author: Martin Panter (martin.panter) *  |
Date: 2016-12-05 22:02 |
The __iter__() method was added by Barry in <http://svn.python.org/view/sandbox/trunk/emailpkg/5_0-exp/email/message.py?r1=57344&r2=57343&pathrev=57344>: “Added an __iter__() to email.message.Message for iterating over the message’s headers.” On the other hand, earlier he said it was ambiguous what the behaviour should be: Issue 1017329. |
|
|
msg282479 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2016-12-05 22:37 |
I had no memory that __iter__ wasn't implemented in python2, thanks Martin. So, this isn't really a bug, though one could argue it should be mentioned explicitly in the 2.7 docs, since it is definitely counter-intuitive to a python programmer. |
|
|
msg382157 - (view) |
Author: Irit Katriel (iritkatriel) *  |
Date: 2020-11-30 15:37 |
Python 2-only issue. |
|
|