|
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"
|
@@ -906,12 +892,28 @@ async def read_response(
|
906 | 892 | ):
|
907 | 893 | """Read the response from a previously sent command"""
|
908 | 894 | read_timeout = timeout if timeout is not None else self.socket_timeout
|
909 |
| - lock_ctxt = self._lock if with_lock else nullcontext |
910 | 895 | 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 | + ) |
915 | 917 | except asyncio.TimeoutError:
|
916 | 918 | if timeout is not None:
|
917 | 919 | # user requested timeout, return None
|
|
0 commit comments