99 _AsyncRESPBase ,
1010 _RESPBase ,
1111)
12- from .socket import SERVER_CLOSED_CONNECTION_ERROR
12+ from .socket import SENTINEL , SERVER_CLOSED_CONNECTION_ERROR
1313
1414
1515class _RESP3Parser (_RESPBase , PushNotificationsParser ):
@@ -28,11 +28,18 @@ def handle_pubsub_push_response(self, response):
2828 logger .debug ("Push response: " + str (response ))
2929 return response
3030
31- def read_response (self , disable_decoding = False , push_request = False ):
31+ def read_response (
32+ self ,
33+ disable_decoding = False ,
34+ push_request = False ,
35+ timeout : Union [float , object ] = SENTINEL ,
36+ ):
3237 pos = self ._buffer .get_pos () if self ._buffer is not None else None
3338 try :
3439 result = self ._read_response (
35- disable_decoding = disable_decoding , push_request = push_request
40+ disable_decoding = disable_decoding ,
41+ push_request = push_request ,
42+ timeout = timeout ,
3643 )
3744 except BaseException :
3845 if self ._buffer is not None :
@@ -48,8 +55,13 @@ def read_response(self, disable_decoding=False, push_request=False):
4855 pass
4956 return result
5057
51- def _read_response (self , disable_decoding = False , push_request = False ):
52- raw = self ._buffer .readline ()
58+ def _read_response (
59+ self ,
60+ disable_decoding = False ,
61+ push_request = False ,
62+ timeout : Union [float , object ] = SENTINEL ,
63+ ):
64+ raw = self ._buffer .readline (timeout = timeout )
5365 if not raw :
5466 raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
5567
@@ -58,7 +70,7 @@ def _read_response(self, disable_decoding=False, push_request=False):
5870 # server returned an error
5971 if byte in (b"-" , b"!" ):
6072 if byte == b"!" :
61- response = self ._buffer .read (int (response ))
73+ response = self ._buffer .read (int (response ), timeout = timeout )
6274 response = response .decode ("utf-8" , errors = "replace" )
6375 error = self .parse_error (response )
6476 # if the error is a ConnectionError, raise immediately so the user
@@ -87,22 +99,22 @@ def _read_response(self, disable_decoding=False, push_request=False):
8799 return response == b"t"
88100 # bulk response
89101 elif byte == b"$" :
90- response = self ._buffer .read (int (response ))
102+ response = self ._buffer .read (int (response ), timeout = timeout )
91103 # verbatim string response
92104 elif byte == b"=" :
93- response = self ._buffer .read (int (response ))[4 :]
105+ response = self ._buffer .read (int (response ), timeout = timeout )[4 :]
94106 # array response
95107 elif byte == b"*" :
96108 response = [
97- self ._read_response (disable_decoding = disable_decoding )
109+ self ._read_response (disable_decoding = disable_decoding , timeout = timeout )
98110 for _ in range (int (response ))
99111 ]
100112 # set response
101113 elif byte == b"~" :
102114 # redis can return unhashable types (like dict) in a set,
103115 # so we return sets as list, all the time, for predictability
104116 response = [
105- self ._read_response (disable_decoding = disable_decoding )
117+ self ._read_response (disable_decoding = disable_decoding , timeout = timeout )
106118 for _ in range (int (response ))
107119 ]
108120 # map response
@@ -112,16 +124,22 @@ def _read_response(self, disable_decoding=False, push_request=False):
112124 # became defined to be left-right in version 3.8
113125 resp_dict = {}
114126 for _ in range (int (response )):
115- key = self ._read_response (disable_decoding = disable_decoding )
127+ key = self ._read_response (
128+ disable_decoding = disable_decoding , timeout = timeout
129+ )
116130 resp_dict [key ] = self ._read_response (
117- disable_decoding = disable_decoding , push_request = push_request
131+ disable_decoding = disable_decoding ,
132+ push_request = push_request ,
133+ timeout = timeout ,
118134 )
119135 response = resp_dict
120136 # push response
121137 elif byte == b">" :
122138 response = [
123139 self ._read_response (
124- disable_decoding = disable_decoding , push_request = push_request
140+ disable_decoding = disable_decoding ,
141+ push_request = push_request ,
142+ timeout = timeout ,
125143 )
126144 for _ in range (int (response ))
127145 ]
0 commit comments