[Python-checkins] python/dist/src/Lib urllib2.py,1.42,1.43

Raymond Hettinger python@rcn.com
Sat, 17 May 2003 15:00:09 -0400


> Log Message:
> Change Request.add_header to call string.capitalize in order to normalize
> headers and not have any dependency on case.  Closes patch #649742.
> 
> Also changed all instances of dict.items to dict.iteritems where appropriate.

Please be careful.  At least two of the iteritems() edits were not appropriate 
because the dictionary's keyset gets updated during the loop.

Please back-out those two changes and re-check the others.

Long before the alpha was released, we went through the whole library
and made these kind of changes.  If something was not changed, then
there was likely a reason for it.  There is a certain element of risk
to making these changes, so every edit was double checked by another 
person and no changes of this type were made after the alpha.  
Also, no changes like this were considered for backporting. 


Raymond


> Index: urllib2.py
> ===================================================================
> RCS file: /cvsroot/python/python/dist/src/Lib/urllib2.py,v
> retrieving revision 1.42
> retrieving revision 1.43
> diff -C2 -d -r1.42 -r1.43
> *** urllib2.py 5 May 2003 04:09:13 -0000 1.42
> --- urllib2.py 12 May 2003 07:29:42 -0000 1.43
> ***************
> *** 194,198 ****
>           self.data = data
>           self.headers = {}
> !         self.headers.update(headers)
>   
>       def __getattr__(self, attr):
> --- 194,199 ----
>           self.data = data
>           self.headers = {}
> !         for key, value in headers.iteritems():
> !             self.add_header(key, value)

self.add_header will capitalize the key and add it back to the dictionary
that is being iterated over.  If the dictionary resizes upon the insertion,
then the key order changes and some keys will be skipped.



> ***************
> *** 1022,1026 ****
>           # then check the size
>           if len(self.cache) == self.max_conns:
> !             for k, v in self.timeout.items():
>                   if v == self.soonest:
>                       del self.cache[k]
> --- 1023,1027 ----
>           # then check the size
>           if len(self.cache) == self.max_conns:
> !             for k, v in self.timeout.iteritems():
>                   if v == self.soonest:
>                       del self.cache[k]

The next line (not shown in the context diff) is:
        del self.timeout[k]
That mutates the dictionary being looped over.