Skip to content

Commit

Permalink
proxy: correct extraction of errno from pysocks ProxyConnectionError
Browse files Browse the repository at this point in the history
Fixes `IndexError: tuple index out of range` trying to extract `errno` from `ProxyConnectionError` from pysocks package, because that exception class lacks `args` attribute.

#170
#202
  • Loading branch information
ivanyu committed Oct 11, 2021
1 parent 4180446 commit dce3376
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ def _get_end2end_headers(response):
return [header for header in list(response.keys()) if header not in hopbyhop]


def _errno_from_exception(e):
# socket.error and common wrap in .args
if len(e.args) > 0:
return e.args[0].errno if isinstance(e.args[0], socket.error) else e.errno

# pysocks.ProxyError wraps in .socket_err
# https://github.com/httplib2/httplib2/pull/202
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


URI = re.compile(r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?")


Expand Down Expand Up @@ -1355,7 +1369,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_ = _errno_from_exception(e)
if errno_ in (errno.ENETUNREACH, errno.EADDRNOTAVAIL) and i < RETRIES:
continue # retry on potentially transient errors
raise
Expand Down

0 comments on commit dce3376

Please sign in to comment.