46
46
else :
47
47
NONBLOCKING_EXCEPTION_ERROR_NUMBERS [ssl .SSLError ] = 2
48
48
49
+ # In Python 2.7 a socket.error is raised for a nonblocking read.
50
+ # The _compat module aliases BlockingIOError to socket.error to be
51
+ # Python 2/3 compatible.
52
+ # However this means that all socket.error exceptions need to be handled
53
+ # properly within these exception handlers.
54
+ # We need to make sure socket.error is included in these handlers and
55
+ # provide a dummy error number that will never match a real exception.
56
+ if socket .error not in NONBLOCKING_EXCEPTION_ERROR_NUMBERS :
57
+ NONBLOCKING_EXCEPTION_ERROR_NUMBERS [socket .error ] = - 999999
58
+
49
59
NONBLOCKING_EXCEPTIONS = tuple (NONBLOCKING_EXCEPTION_ERROR_NUMBERS .keys ())
50
60
51
61
if HIREDIS_AVAILABLE :
@@ -184,7 +194,7 @@ def _read_from_socket(self, length=None, timeout=SENTINEL,
184
194
return True
185
195
except socket .timeout :
186
196
if raise_on_timeout :
187
- raise
197
+ raise TimeoutError ( "Timeout reading from socket" )
188
198
return False
189
199
except NONBLOCKING_EXCEPTIONS as ex :
190
200
# if we're in nonblocking mode and the recv raises a
@@ -194,7 +204,8 @@ def _read_from_socket(self, length=None, timeout=SENTINEL,
194
204
allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS .get (ex .__class__ , - 1 )
195
205
if not raise_on_timeout and ex .errno == allowed :
196
206
return False
197
- raise
207
+ raise ConnectionError ("Error while reading from socket: %s" %
208
+ (ex .args ,))
198
209
finally :
199
210
if custom_timeout :
200
211
sock .settimeout (self .socket_timeout )
@@ -414,7 +425,7 @@ def read_from_socket(self, timeout=SENTINEL, raise_on_timeout=True):
414
425
return True
415
426
except socket .timeout :
416
427
if raise_on_timeout :
417
- raise
428
+ raise TimeoutError ( "Timeout reading from socket" )
418
429
return False
419
430
except NONBLOCKING_EXCEPTIONS as ex :
420
431
# if we're in nonblocking mode and the recv raises a
@@ -424,7 +435,8 @@ def read_from_socket(self, timeout=SENTINEL, raise_on_timeout=True):
424
435
allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS .get (ex .__class__ , - 1 )
425
436
if not raise_on_timeout and ex .errno == allowed :
426
437
return False
427
- raise
438
+ raise ConnectionError ("Error while reading from socket: %s" %
439
+ (ex .args ,))
428
440
finally :
429
441
if custom_timeout :
430
442
sock .settimeout (self ._socket_timeout )
0 commit comments