Skip to content

Commit 423c0e6

Browse files
committed
Provide asynccontextmanager for python 3.6
1 parent 966052c commit 423c0e6

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

tests/test_asyncio/conftest.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import random
23
import sys
34
from typing import Union
@@ -243,3 +244,41 @@ async def wait_for_command(
243244
return monitor_response
244245
if key in monitor_response["command"]:
245246
return None
247+
248+
249+
# python 3.6 doesn't have the asynccontextmanager decorator. Provide it here.
250+
class AsyncContextManager:
251+
def __init__(self, async_generator):
252+
self.gen = async_generator
253+
254+
async def __aenter__(self):
255+
try:
256+
return await self.gen.__anext__()
257+
except StopAsyncIteration as err:
258+
raise RuntimeError("Pickles") from err
259+
260+
async def __aexit__(self, exc_type, exc_inst, tb):
261+
if exc_type:
262+
await self.gen.athrow(exc_type, exc_inst, tb)
263+
return True
264+
try:
265+
await self.gen.__anext__()
266+
except StopAsyncIteration:
267+
return
268+
raise RuntimeError("More pickles")
269+
270+
271+
if sys.version_info[0:2] == (3, 6):
272+
273+
def asynccontextmanager(func):
274+
@functools.wraps(func)
275+
def wrapper(*args, **kwargs):
276+
return AsyncContextManager(func(*args, **kwargs))
277+
278+
return wrapper
279+
280+
else:
281+
from contextlib import asynccontextmanager as _asynccontextmanager
282+
283+
def asynccontextmanager(func):
284+
return _asynccontextmanager(func)

tests/test_asyncio/test_connection_pool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import contextlib
32
import os
43
import re
54
import sys
@@ -18,6 +17,7 @@
1817
from tests.conftest import skip_if_redis_enterprise, skip_if_server_version_lt
1918

2019
from .compat import mock
20+
from .conftest import asynccontextmanager
2121
from .test_pubsub import wait_for_message
2222

2323

@@ -115,7 +115,7 @@ async def can_read(self, timeout: float = 0):
115115

116116

117117
class TestConnectionPool:
118-
@contextlib.asynccontextmanager
118+
@asynccontextmanager
119119
async def get_pool(
120120
self,
121121
connection_kwargs=None,
@@ -197,7 +197,7 @@ async def test_repr_contains_db_info_unix(self):
197197

198198

199199
class TestBlockingConnectionPool:
200-
@contextlib.asynccontextmanager
200+
@asynccontextmanager
201201
async def get_pool(self, connection_kwargs=None, max_connections=10, timeout=20):
202202
connection_kwargs = connection_kwargs or {}
203203
pool = redis.BlockingConnectionPool(

0 commit comments

Comments
 (0)