A couple of Python 'Features'

Alex Martelli aleaxit at yahoo.com
Sat Sep 4 08:13:23 EDT 2004


Michael Foord <fuzzyman at gmail.com> wrote:

> I've come across a couple of 'features' in Python standard libraries -
> and I'm not sure if they're meant to be there... or if they're bugs...
> 
> One in urllib2 and one in cgi.
> 
> >>> from urllib2 import urlopen
> >>> a = urlopen('http://www.voidspace.org.uk')
> >>> i = a.info()
> >>> for entry in i: print entry
> Traceback (most recent call last):
>   File "<pyshell#11>", line 1, in -toplevel-
>     for entry in i: print entry
>   File "D:\PYTHON23\lib\rfc822.py", line 390, in __getitem__
>     return self.dict[name.lower()]
> AttributeError: 'int' object has no attribute 'lower'

Note that the problem was in rfc822 -- urllib2 was just giving you an
rfc822 message object, which then got erroneously identified as a
sequence in the implicit iter() call in the for statement.

> 
> and in cgi (a result that happens in live CGI as well as interactive
> sessions....) :
> 
> >>> import cgi
> >>> a = cgi.FieldStorage()
> >>> for entry in a: print entry
> 
> Traceback (most recent call last):
>   File "<pyshell#3>", line 1, in ?
>     for entry in a: print entry
>   File "C:\PYTHON22\lib\cgi.py", line 550, in __getitem_
>     raise KeyError,
>  keyKeyError: 0
> >>> 
> 
> You can get round the cgi bug because :
> for entry in a.keys(): print entry
> works fine - but it's a bit annoying.
> 
> (Admittedly my server has python 2.2, but the urllib2 bug occurs in
> Python 2.3.4 as well I think).

Yes, 2.3 fixed the problem with cgi.fieldstorage (by adding an __iter__
that just returns iter(self.keys())...!-) but even 2.4 alpha 3 still has
the problem with rfc822 (which could and IMHO should be fixed similarly,
returning an iter(self.dict) from the __iter__ method).
If you post the rfc822 bug to the Python bug tracker it can probably get
fixed soon (of course it will need a new 2.3 release to get the fix out,
but at least 2.4 beta 1 won't have the problem any more).


Alex



More information about the Python-list mailing list