Commits

Chris Adams committed 676e2ad

_conn_request: fix for AttributeErrors caused by closed connections

In some situations it's possible for conn.getresponse() to be called when
conn.sock is None. This patch allows the retry mechanism to work and raises a
more accurate exception (httplib.NotConnected) when it fails.

See http://code.google.com/p/httplib2/issues/detail?id=96

Comments (0)

Files changed (1)

python2/httplib2/__init__.py

                 # Just because the server closed the connection doesn't apparently mean
                 # that the server didn't send a response.
                 pass
+
             try:
                 response = conn.getresponse()
-            except (socket.error, httplib.HTTPException):
+            except (socket.error, httplib.HTTPException, AttributeError), e:
                 if i == 0:
                     conn.close()
                     conn.connect()
                     continue
+                elif isinstance(e, AttributeError) and \
+                    getattr(conn, "sock", False) is None:
+                        # There are certain pathological situations where
+                        # httplib neither connects nor raises an exception. In
+                        # this case we'd like to provide a more meaningful
+                        # error message than the .makefile AttributeError
+                        # conn.getresponse() will throw the first time it
+                        # attempts to use the socket (see #96 and its
+                        # duplicates):
+
+                        raise httplib.NotConnected()
                 else:
-                    raise
+                    raise e
             else:
                 content = ""
                 if method == "HEAD":
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.