@@ -146,7 +146,7 @@ def decode(self, value: EncodableT, force=False) -> EncodableT:
146
146
class BaseParser :
147
147
"""Plain Python parsing class"""
148
148
149
- __slots__ = "_stream" , "_read_size"
149
+ __slots__ = "_stream" , "_read_size" , "_connected"
150
150
151
151
EXCEPTION_CLASSES : ExceptionMappingT = {
152
152
"ERR" : {
@@ -177,6 +177,7 @@ class BaseParser:
177
177
def __init__ (self , socket_read_size : int ):
178
178
self ._stream : Optional [asyncio .StreamReader ] = None
179
179
self ._read_size = socket_read_size
180
+ self ._connected = False
180
181
181
182
def __del__ (self ):
182
183
try :
@@ -213,7 +214,7 @@ async def read_response(
213
214
class PythonParser (BaseParser ):
214
215
"""Plain Python parsing class"""
215
216
216
- __slots__ = BaseParser . __slots__ + ("encoder" , "_buffer" , "_pos" , "_chunks" )
217
+ __slots__ = ("encoder" , "_buffer" , "_pos" , "_chunks" )
217
218
218
219
def __init__ (self , socket_read_size : int ):
219
220
super ().__init__ (socket_read_size )
@@ -231,28 +232,28 @@ def on_connect(self, connection: "Connection"):
231
232
self ._stream = connection ._reader
232
233
if self ._stream is None :
233
234
raise RedisError ("Buffer is closed." )
234
-
235
235
self .encoder = connection .encoder
236
+ self ._clear ()
237
+ self ._connected = True
236
238
237
239
def on_disconnect (self ):
238
240
"""Called when the stream disconnects"""
239
- if self ._stream is not None :
240
- self ._stream = None
241
- self .encoder = None
242
- self ._clear ()
241
+ self ._connected = False
243
242
244
243
async def can_read_destructive (self ) -> bool :
244
+ if not self ._connected :
245
+ raise RedisError ("Buffer is closed." )
245
246
if self ._buffer :
246
247
return True
247
- if self ._stream is None :
248
- raise RedisError ("Buffer is closed." )
249
248
try :
250
249
async with async_timeout (0 ):
251
250
return await self ._stream .read (1 )
252
251
except asyncio .TimeoutError :
253
252
return False
254
253
255
254
async def read_response (self , disable_decoding : bool = False ):
255
+ if not self ._connected :
256
+ raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
256
257
if self ._chunks :
257
258
# augment parsing buffer with previously read data
258
259
self ._buffer += b"" .join (self ._chunks )
@@ -266,8 +267,6 @@ async def read_response(self, disable_decoding: bool = False):
266
267
async def _read_response (
267
268
self , disable_decoding : bool = False
268
269
) -> Union [EncodableT , ResponseError , None ]:
269
- if not self ._stream or not self .encoder :
270
- raise ConnectionError (SERVER_CLOSED_CONNECTION_ERROR )
271
270
raw = await self ._readline ()
272
271
response : Any
273
272
byte , response = raw [:1 ], raw [1 :]
@@ -354,14 +353,13 @@ async def _readline(self) -> bytes:
354
353
class HiredisParser (BaseParser ):
355
354
"""Parser class for connections using Hiredis"""
356
355
357
- __slots__ = BaseParser . __slots__ + ("_reader" , "_connected" )
356
+ __slots__ = ("_reader" ,)
358
357
359
358
def __init__ (self , socket_read_size : int ):
360
359
if not HIREDIS_AVAILABLE :
361
360
raise RedisError ("Hiredis is not available." )
362
361
super ().__init__ (socket_read_size = socket_read_size )
363
362
self ._reader : Optional [hiredis .Reader ] = None
364
- self ._connected : bool = False
365
363
366
364
def on_connect (self , connection : "Connection" ):
367
365
self ._stream = connection ._reader
0 commit comments