Skip to content

Commit b7bbcc8

Browse files
committed
expand with statement to avoid invoking null context managers.
remove nullcontext
1 parent 63972b6 commit b7bbcc8

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

redis/asyncio/connection.py

+21-19
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"
@@ -895,12 +881,28 @@ async def read_response(
895881
):
896882
"""Read the response from a previously sent command"""
897883
read_timeout = timeout if timeout is not None else self.socket_timeout
898-
lock_ctxt = self._lock if with_lock else nullcontext
899884
try:
900-
async with lock_ctxt, async_timeout.timeout(read_timeout):
901-
response = await self._parser.read_response(
902-
disable_decoding=disable_decoding
903-
)
885+
if with_lock:
886+
if read_timeout is not None:
887+
async with self._lock, async_timeout.timeout(read_timeout):
888+
response = await self._parser.read_response(
889+
disable_decoding=disable_decoding
890+
)
891+
else:
892+
async with self._lock:
893+
response = await self._parser.read_response(
894+
disable_decoding=disable_decoding
895+
)
896+
else:
897+
if read_timeout is not None:
898+
async with async_timeout.timeout(read_timeout):
899+
response = await self._parser.read_response(
900+
disable_decoding=disable_decoding
901+
)
902+
else:
903+
response = await self._parser.read_response(
904+
disable_decoding=disable_decoding
905+
)
904906
except asyncio.TimeoutError:
905907
if timeout is not None:
906908
# user requested timeout, return None

0 commit comments

Comments
 (0)