Skip to content

Commit

Permalink
fix: replace deprecated socket.error with OSError (#1161)
Browse files Browse the repository at this point in the history
Fixes #1148 馃
  • Loading branch information
dossett committed Jan 13, 2021
1 parent ae9cd99 commit b7b9986
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions googleapiclient/http.py
Expand Up @@ -178,14 +178,13 @@ def _retry_request(
except _ssl_SSLError as ssl_error: except _ssl_SSLError as ssl_error:
exception = ssl_error exception = ssl_error
except socket.timeout as socket_timeout: except socket.timeout as socket_timeout:
# It's important that this be before socket.error as it's a subclass # Needs to be before socket.error as it's a subclass of OSError
# socket.timeout has no errorcode # socket.timeout has no errorcode
exception = socket_timeout exception = socket_timeout
except ConnectionError as connection_error: except ConnectionError as connection_error:
# Needs to be before socket.error as it's a subclass of # Needs to be before socket.error as it's a subclass of OSError
# OSError (socket.error)
exception = connection_error exception = connection_error
except socket.error as socket_error: except OSError as socket_error:
# errno's contents differ by platform, so we have to match by name. # errno's contents differ by platform, so we have to match by name.
# Some of these same errors may have been caught above, e.g. ECONNRESET *should* be # Some of these same errors may have been caught above, e.g. ECONNRESET *should* be
# raised as a ConnectionError, but some libraries will raise it as a socket.error # raised as a ConnectionError, but some libraries will raise it as a socket.error
Expand Down
10 changes: 5 additions & 5 deletions tests/test_http.py
Expand Up @@ -137,22 +137,22 @@ def request(self, *args, **kwargs):
elif self.num_errors == 4: elif self.num_errors == 4:
ex = httplib2.ServerNotFoundError() ex = httplib2.ServerNotFoundError()
elif self.num_errors == 3: elif self.num_errors == 3:
ex = socket.error() ex = OSError()
ex.errno = socket.errno.EPIPE ex.errno = socket.errno.EPIPE
elif self.num_errors == 2: elif self.num_errors == 2:
ex = ssl.SSLError() ex = ssl.SSLError()
else: else:
# Initialize the timeout error code to the platform's error code. # Initialize the timeout error code to the platform's error code.
try: try:
# For Windows: # For Windows:
ex = socket.error() ex = OSError()
ex.errno = socket.errno.WSAETIMEDOUT ex.errno = socket.errno.WSAETIMEDOUT
except AttributeError: except AttributeError:
# For Linux/Mac: # For Linux/Mac:
if PY3: if PY3:
ex = socket.timeout() ex = socket.timeout()
else: else:
ex = socket.error() ex = OSError()
ex.errno = socket.errno.ETIMEDOUT ex.errno = socket.errno.ETIMEDOUT


self.num_errors -= 1 self.num_errors -= 1
Expand All @@ -170,7 +170,7 @@ def request(self, *args, **kwargs):
return httplib2.Response(self.success_json), self.success_data return httplib2.Response(self.success_json), self.success_data
else: else:
self.num_errors -= 1 self.num_errors -= 1
ex = socket.error() ex = OSError()
# set errno to a non-retriable value # set errno to a non-retriable value
try: try:
# For Windows: # For Windows:
Expand Down Expand Up @@ -943,7 +943,7 @@ def test_no_retry_connection_errors(self):
) )
request._sleep = lambda _x: 0 # do nothing request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10 request._rand = lambda: 10
with self.assertRaises(socket.error): with self.assertRaises(OSError):
response = request.execute(num_retries=3) response = request.execute(num_retries=3)


def test_retry_connection_errors_non_resumable(self): def test_retry_connection_errors_non_resumable(self):
Expand Down

0 comments on commit b7b9986

Please sign in to comment.