]> jfr.im git - z_archive/twitter.git/commitdiff
TwitterCall._handle_response(): if reading from handle raises httlib.IncompleteRead...
authorDaniel Murray <redacted>
Wed, 13 Feb 2013 07:05:32 +0000 (15:05 +0800)
committerDaniel Murray <redacted>
Wed, 13 Feb 2013 07:05:32 +0000 (15:05 +0800)
twitter/api.py

index 6571735b1d9498e1f97d5bfba3e9a2be9055c0be..c61ffc50c56883a48ac048f13c0b2a2f8cd9fac1 100644 (file)
@@ -15,6 +15,7 @@ from twitter.auth import NoAuth
 
 import re
 import gzip
+import httplib
 
 try:
     import json
@@ -201,14 +202,17 @@ class TwitterCall(object):
             handle = urllib_request.urlopen(req, **kwargs)
             if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']:
                 return handle
-            elif handle.info().get('Content-Encoding') == 'gzip':
+            try:
+                data = handle.read()
+            except httplib.IncompleteRead, e:
+                # Even if we don't get all the bytes we should have there
+                # may be a complete response in e.partial
+                data = e.partial
+            if handle.info().get('Content-Encoding') == 'gzip':
                 # Handle gzip decompression
-                buf = StringIO(handle.read())
+                buf = StringIO(data)
                 f = gzip.GzipFile(fileobj=buf)
                 data = f.read()
-            else:
-                data = handle.read()
-
             if "json" == self.format:
                 res = json.loads(data.decode('utf8'))
                 return wrap_response(res, handle.headers)