User Tools

Site Tools


python:bugsvalue

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:bugsvalue [2013/03/16 17:41] (current)
Line 1: Line 1:
 +=== HOW TO FIX: Error code:  invalid literal for int() with base 16 ===
  
 +
 +This bug appears in httplib.py, at least in python 2.5. For some interogations,​ it will cause a msg as: **Error code:  invalid literal for int() with base 16** to be thrown. Discovered by me in urllib2.urlopen when I tried to grab http://​www.bnro.ro/​nbrfxrates.xml.
 +
 +
 +Explanations:​ In HTTPResponse._read_chunked the chunk length is not checked to be a valid integer, and a ValueError will be raised in such a case.
 +
 +
 +The fix is:
 +
 +1. Open the httplib.py file in an editor (e.g. /​usr/​local/​lib/​python2.5/​httplib.py) \\ 
 +2. Around line 546 localize the code:
 +
 +<code python>
 +    def _read_chunked(self,​ amt):
 +        assert self.chunked != _UNKNOWN
 +        chunk_left = self.chunk_left
 +        value = ''​
 +
 +        # XXX This accumulates chunks by repeated string concatenation,​
 +        # which is not efficient as the number or size of chunks gets big.
 +        while True:
 +            if chunk_left is None:
 +                line = self.fp.readline()
 +                i = line.find(';'​)
 +                if i >= 0:
 +                    line = line[:i] # strip chunk-extensions
 +                chunk_left = int(line, 16)
 +</​code>​
 +
 +and replace the chunk_left ... line with:
 +
 +<​code>​
 +                # fix for Error code:  invalid literal for int() with base 16: ''​
 +                try:
 +                    chunk_left = int(line, 16)          ​
 +                except ValueError, msg:          ​
 +                    self.close() ​                   ​
 +                    return value
 +
 +</​code>​
python/bugsvalue.txt ยท Last modified: 2013/03/16 17:41 (external edit)