|
1 | 1 | import asyncio
|
2 |
| -import contextlib |
3 | 2 | import copy
|
4 | 3 | import enum
|
5 | 4 | import inspect
|
|
55 | 54 | if HIREDIS_AVAILABLE:
|
56 | 55 | import hiredis
|
57 | 56 |
|
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 |
| - |
71 | 57 | SYM_STAR = b"*"
|
72 | 58 | SYM_DOLLAR = b"$"
|
73 | 59 | SYM_CRLF = b"\r\n"
|
@@ -895,12 +881,28 @@ async def read_response(
|
895 | 881 | ):
|
896 | 882 | """Read the response from a previously sent command"""
|
897 | 883 | read_timeout = timeout if timeout is not None else self.socket_timeout
|
898 |
| - lock_ctxt = self._lock if with_lock else nullcontext |
899 | 884 | 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 | + ) |
904 | 906 | except asyncio.TimeoutError:
|
905 | 907 | if timeout is not None:
|
906 | 908 | # user requested timeout, return None
|
|
0 commit comments