Fix retying on socket.timeout (#495)
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index 29aef0e..221b1c2 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -163,10 +163,14 @@
# Retry on SSL errors and socket timeout errors.
except _ssl_SSLError as ssl_error:
exception = ssl_error
+ except socket.timeout as socket_timeout:
+ # It's important that this be before socket.error as it's a subclass
+ # socket.timeout has no errorcode
+ exception = socket_timeout
except socket.error as socket_error:
# errno's contents differ by platform, so we have to match by name.
- if socket.errno.errorcode.get(socket_error.errno) not in (
- 'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED', ):
+ if socket.errno.errorcode.get(socket_error.errno) not in {
+ 'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED'}:
raise
exception = socket_error
diff --git a/tests/test_http.py b/tests/test_http.py
index f337401..f6beff7 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -125,24 +125,26 @@
return httplib2.Response(self.success_json), self.success_data
else:
self.num_errors -= 1
- if self.num_errors == 1:
+ if self.num_errors == 1: # initial == 2
raise ssl.SSLError()
- else:
- if PY3:
- ex = TimeoutError()
- else:
- ex = socket.error()
+ else: # initial != 2
if self.num_errors == 2:
- #first try a broken pipe error (#218)
+ # first try a broken pipe error (#218)
+ ex = socket.error()
ex.errno = socket.errno.EPIPE
else:
# Initialize the timeout error code to the platform's error code.
try:
# For Windows:
+ ex = socket.error()
ex.errno = socket.errno.WSAETIMEDOUT
except AttributeError:
# For Linux/Mac:
- ex.errno = socket.errno.ETIMEDOUT
+ if PY3:
+ ex = socket.timeout()
+ else:
+ ex = socket.error()
+ ex.errno = socket.errno.ETIMEDOUT
# Now raise the correct error.
raise ex