-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support with or without
aclose()
on all inputs/sources
Note: also applying to outputs so async_generator dep no longer needed.
- Loading branch information
1 parent
9d862bb
commit 32dda80
Showing
17 changed files
with
207 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import ( | ||
AsyncGenerator, | ||
AsyncIterable, | ||
AsyncIterator, | ||
Awaitable, | ||
Protocol, | ||
TypeVar, | ||
Union, | ||
runtime_checkable, | ||
) | ||
|
||
from contextlib import asynccontextmanager | ||
|
||
_T_co = TypeVar("_T_co", covariant=True) | ||
|
||
@asynccontextmanager | ||
async def safe_aclosing( | ||
obj: Union[AsyncIterable[_T_co], AsyncIterator[_T_co]] | ||
) -> AsyncGenerator[AsyncIterator[_T_co], None]: | ||
if not isinstance(obj, AsyncIterator): | ||
obj = obj.__aiter__() | ||
try: | ||
yield obj | ||
finally: | ||
await safe_aclose(obj) | ||
|
||
async def safe_aclose(obj: AsyncIterator[_T_co]) -> None: | ||
if isinstance(obj, _SupportsAclose): | ||
await obj.aclose() | ||
|
||
@runtime_checkable | ||
class _SupportsAclose(Protocol): | ||
def aclose(self) -> Awaitable[object]: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import string | ||
from functools import wraps | ||
|
||
import pytest | ||
import trio | ||
|
||
from slurry._utils import safe_aclose | ||
|
||
def fixture_gen_with_and_without_aclose(async_gen): | ||
|
||
def fixture_func(_with_aclose): | ||
if _with_aclose: | ||
new_async_gen = async_gen | ||
else: | ||
@wraps(async_gen) | ||
def new_async_gen(*args, **kwargs): | ||
return AsyncIteratorWithoutAclose(async_gen(*args, **kwargs)) | ||
return new_async_gen | ||
|
||
fixture_func.__name__ = async_gen.__name__ | ||
fixture_func.__qualname__ = async_gen.__name__ | ||
|
||
return pytest.fixture(fixture_func) | ||
|
||
@pytest.fixture(params=[True, False], ids=["with_aclose", "without_aclose"]) | ||
def _with_aclose(request): | ||
return request.param | ||
|
||
@fixture_gen_with_and_without_aclose | ||
async def produce_increasing_integers(interval, *, max=3, delay=0): | ||
await trio.sleep(delay) | ||
for i in range(max): | ||
yield i | ||
if i == max-1: | ||
break | ||
await trio.sleep(interval) | ||
|
||
@fixture_gen_with_and_without_aclose | ||
async def produce_alphabet(interval, *, max=3, delay=0): | ||
await trio.sleep(delay) | ||
for i, c in enumerate(string.ascii_lowercase): | ||
yield c | ||
if i == max - 1: | ||
break | ||
await trio.sleep(interval) | ||
|
||
@pytest.fixture() | ||
def spam_wait_spam_integers(produce_increasing_integers): | ||
async def spam_wait_spam_integers(interval): | ||
async for i in produce_increasing_integers(.1, max=5, delay=.1): | ||
yield i | ||
await trio.sleep(interval) | ||
async for i in produce_increasing_integers(.1, max=5, delay=.1): | ||
yield i | ||
|
||
return spam_wait_spam_integers | ||
|
||
@fixture_gen_with_and_without_aclose | ||
async def produce_mappings(interval): | ||
vehicles = [ | ||
{'vehicle': 'motorcycle'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'motorcycle'}, | ||
{'vehicle': 'autocamper'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'truck'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'motorcycle'}, | ||
] | ||
|
||
for i, vehicle in enumerate(vehicles): | ||
vehicle['number'] = i | ||
yield vehicle | ||
await trio.sleep(interval) | ||
|
||
class AsyncIteratorWithoutAclose: | ||
def __init__(self, source_aiterable): | ||
self.source_aiter = source_aiterable.__aiter__() | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
try: | ||
return await self.source_aiter.__anext__() | ||
except StopAsyncIteration: | ||
await safe_aclose(self.source_aiter) | ||
raise |
Oops, something went wrong.