Skip to content

Commit

Permalink
Add in missing anext function for python 3.8 and 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseBlaauwbroek committed Oct 16, 2023
1 parent e9f11e9 commit 13da544
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pytact/data_reader.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import threading
import subprocess
import shutil
import time
import sys

T = TypeVar('T')
class TupleLike():
Expand Down Expand Up @@ -1465,6 +1466,27 @@ async def record_lowlevel_generator(
response.write_packed(record_file)
yield

# Taken from https://github.com/python/cpython/pull/8895
# Can be removed once python 3.9 is no longer supported
if sys.version_info.major == 3 and sys.version_info.minor < 10:
_NOT_PROVIDED = object()
async def anext(async_iterator, default=_NOT_PROVIDED):
"""anext(async_iterator[, default])
Return the next item from the async iterator.
If default is given and the iterator is exhausted,
it is returned instead of raising StopAsyncIteration.
"""
from collections.abc import AsyncIterator
if not isinstance(async_iterator, AsyncIterator):
raise TypeError(f'anext expected an AsyncIterator, got {type(async_iterator)}')
anxt = type(async_iterator).__anext__
try:
return await anxt(async_iterator)
except StopAsyncIteration:
if default is _NOT_PROVIDED:
raise
return default

@dataclass
class _MutableBox:
contents: Any
Expand Down

0 comments on commit 13da544

Please sign in to comment.