Skip to content

Commit 409aedc

Browse files
committed
expand with statement to avoid invoking null context managers.
remove nullcontext
1 parent 8ce9258 commit 409aedc

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

redis/asyncio/connection.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import contextlib
32
import copy
43
import enum
54
import inspect
@@ -55,19 +54,6 @@
5554
if HIREDIS_AVAILABLE:
5655
import hiredis
5756

58-
if sys.version_info[:2] >= (3, 10):
59-
nullcontext = contextlib.nullcontext()
60-
else:
61-
62-
class NullContext:
63-
async def __aenter__(self):
64-
pass
65-
66-
async def __aexit__(self, *args):
67-
pass
68-
69-
nullcontext = NullContext()
70-
7157
SYM_STAR = b"*"
7258
SYM_DOLLAR = b"$"
7359
SYM_CRLF = b"\r\n"
@@ -906,12 +892,28 @@ async def read_response(
906892
):
907893
"""Read the response from a previously sent command"""
908894
read_timeout = timeout if timeout is not None else self.socket_timeout
909-
lock_ctxt = self._lock if with_lock else nullcontext
910895
try:
911-
async with lock_ctxt, async_timeout.timeout(read_timeout):
912-
response = await self._parser.read_response(
913-
disable_decoding=disable_decoding
914-
)
896+
if with_lock:
897+
if read_timeout is not None:
898+
async with self._lock, async_timeout.timeout(read_timeout):
899+
response = await self._parser.read_response(
900+
disable_decoding=disable_decoding
901+
)
902+
else:
903+
async with self._lock:
904+
response = await self._parser.read_response(
905+
disable_decoding=disable_decoding
906+
)
907+
else:
908+
if read_timeout is not None:
909+
async with async_timeout.timeout(read_timeout):
910+
response = await self._parser.read_response(
911+
disable_decoding=disable_decoding
912+
)
913+
else:
914+
response = await self._parser.read_response(
915+
disable_decoding=disable_decoding
916+
)
915917
except asyncio.TimeoutError:
916918
if timeout is not None:
917919
# user requested timeout, return None

0 commit comments

Comments
 (0)