19
19
SERVER_CLOSED_CONNECTION_ERROR ,
20
20
)
21
21
22
+ # Used to signal that hiredis-py does not have enough data to parse.
23
+ # Using `False` or `None` is not reliable, given that the parser can
24
+ # return `False` or `None` for legitimate reasons from RESP payloads.
25
+ NOT_ENOUGH_DATA = object ()
26
+
22
27
23
28
class _HiredisReaderArgs (TypedDict , total = False ):
24
29
protocolError : Callable [[str ], Exception ]
@@ -51,25 +56,26 @@ def on_connect(self, connection, **kwargs):
51
56
"protocolError" : InvalidResponse ,
52
57
"replyError" : self .parse_error ,
53
58
"errors" : connection .encoder .encoding_errors ,
59
+ "notEnoughData" : NOT_ENOUGH_DATA ,
54
60
}
55
61
56
62
if connection .encoder .decode_responses :
57
63
kwargs ["encoding" ] = connection .encoder .encoding
58
64
self ._reader = hiredis .Reader (** kwargs )
59
- self ._next_response = False
65
+ self ._next_response = NOT_ENOUGH_DATA
60
66
61
67
def on_disconnect (self ):
62
68
self ._sock = None
63
69
self ._reader = None
64
- self ._next_response = False
70
+ self ._next_response = NOT_ENOUGH_DATA
65
71
66
72
def can_read (self , timeout ):
67
73
if not self ._reader :
68
74
raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
69
75
70
- if self ._next_response is False :
76
+ if self ._next_response is NOT_ENOUGH_DATA :
71
77
self ._next_response = self ._reader .gets ()
72
- if self ._next_response is False :
78
+ if self ._next_response is NOT_ENOUGH_DATA :
73
79
return self .read_from_socket (timeout = timeout , raise_on_timeout = False )
74
80
return True
75
81
@@ -108,17 +114,17 @@ def read_response(self, disable_decoding=False):
108
114
raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
109
115
110
116
# _next_response might be cached from a can_read() call
111
- if self ._next_response is not False :
117
+ if self ._next_response is not NOT_ENOUGH_DATA :
112
118
response = self ._next_response
113
- self ._next_response = False
119
+ self ._next_response = NOT_ENOUGH_DATA
114
120
return response
115
121
116
122
if disable_decoding :
117
123
response = self ._reader .gets (False )
118
124
else :
119
125
response = self ._reader .gets ()
120
126
121
- while response is False :
127
+ while response is NOT_ENOUGH_DATA :
122
128
self .read_from_socket ()
123
129
if disable_decoding :
124
130
response = self ._reader .gets (False )
@@ -156,6 +162,7 @@ def on_connect(self, connection):
156
162
kwargs : _HiredisReaderArgs = {
157
163
"protocolError" : InvalidResponse ,
158
164
"replyError" : self .parse_error ,
165
+ "notEnoughData" : NOT_ENOUGH_DATA ,
159
166
}
160
167
if connection .encoder .decode_responses :
161
168
kwargs ["encoding" ] = connection .encoder .encoding
@@ -170,7 +177,7 @@ def on_disconnect(self):
170
177
async def can_read_destructive (self ):
171
178
if not self ._connected :
172
179
raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
173
- if self ._reader .gets ():
180
+ if self ._reader .gets () is not NOT_ENOUGH_DATA :
174
181
return True
175
182
try :
176
183
async with async_timeout (0 ):
@@ -200,7 +207,7 @@ async def read_response(
200
207
response = self ._reader .gets (False )
201
208
else :
202
209
response = self ._reader .gets ()
203
- while response is False :
210
+ while response is NOT_ENOUGH_DATA :
204
211
await self .read_from_socket ()
205
212
if disable_decoding :
206
213
response = self ._reader .gets (False )
0 commit comments