Skip to content

Commit

Permalink
Add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Sep 7, 2023
1 parent 05bfbf0 commit b623769
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/asynkit/coroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import (
Any,
AsyncGenerator,
AsyncIterable,
Awaitable,
Callable,
Coroutine,
Expand Down Expand Up @@ -37,6 +38,7 @@
"coro_is_suspended",
"coro_is_finished",
"coro_iter",
"aiter_sync",
"await_sync",
"SynchronousError",
"SynchronousAbort",
Expand Down Expand Up @@ -541,3 +543,18 @@ async def helper(*args: P.args, **kwargs: P.kwargs) -> T:
return func(*args, **kwargs)

return helper


def aiter_sync(async_iterable: AsyncIterable[T]) -> Generator[T, None, None]:
"""Iterate synchronously over an async itarable"""
ai = async_iterable.__aiter__()

# a helper ensures that we have a coroutine, not just an Awaitable
async def helper() -> T:
return await ai.__anext__()

try:
while True:
yield await_sync(helper())
except StopAsyncIteration:
pass
9 changes: 9 additions & 0 deletions tests/test_coro.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,15 @@ def test_noexit(self):
assert err.match("failed to complete synchronously")


def test_aiter_sync():
async def agen():
for i in range(5):
yield i

gen = asynkit.aiter_sync(agen())
assert list(gen) == list(range(5))


class TestCoroAwait:
"""
These tests test the behaviour of a coroutine wrapped in `coro_await`
Expand Down

0 comments on commit b623769

Please sign in to comment.