Skip to content

Commit

Permalink
Handle PySocks ProxyConnectionError
Browse files Browse the repository at this point in the history
In the presence of PySocks, httplib2 falls back on it instead of its own bundled `socks.py`. However, exception `ProxyConnectionError` produced by PySocks is a bit different that expected, it lacks `args`. An attempt to extract `errno` from it fails with `IndexError: tuple index out of range` (see #170). The patch addresses this.
  • Loading branch information
ivanyu committed Oct 3, 2021
1 parent ddd0c68 commit 832d456
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ def _conn_request(self, conn, request_uri, method, body, headers):
conn.close()
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
except socket.error as e:
errno_ = e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno
errno_ = self._errno_from_exception(e)
if errno_ in (errno.ENETUNREACH, errno.EADDRNOTAVAIL) and i < RETRIES:
continue # retry on potentially transient errors
raise
Expand Down Expand Up @@ -1413,6 +1413,17 @@ def _conn_request(self, conn, request_uri, method, body, headers):
break
return (response, content)

@staticmethod
def _errno_from_exception(e):
if len(e.args) > 0:
return e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno

if hasattr(e, 'socket_err'):
e_int = e.socket_err
return e_int.args[0].errno if isinstance(e_int.args[0], socket.error) else e_int.errno

return None

def _request(
self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey,
):
Expand Down

0 comments on commit 832d456

Please sign in to comment.