|
10 | 10 | This is the pure Python implementation of the module.
|
11 | 11 | """
|
12 | 12 |
|
13 |
| -__all__ = ['abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', |
14 |
| - 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', |
15 |
| - 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', |
16 |
| - 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', |
17 |
| - 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', |
18 |
| - 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', |
19 |
| - 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', |
20 |
| - 'setitem', 'sub', 'truediv', 'truth', 'xor'] |
| 13 | +__all__ = [ |
| 14 | + 'abs', 'add', 'aiter', 'anext', 'and_', 'attrgetter', 'concat', 'contains', |
| 15 | + 'countOf', 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', |
| 16 | + 'iand', 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', |
| 17 | + 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', 'is_', |
| 18 | + 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', 'length_hint', |
| 19 | + 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', |
| 20 | + 'or_', 'pos', 'pow', 'rshift', 'setitem', 'sub', 'truediv', 'truth', 'xor', |
| 21 | +] |
21 | 22 |
|
22 | 23 | from builtins import abs as _abs
|
| 24 | +from collections.abc import AsyncIterable, AsyncIterator |
23 | 25 |
|
24 | 26 |
|
25 | 27 | # Comparison Operations *******************************************************#
|
@@ -408,6 +410,55 @@ def ixor(a, b):
|
408 | 410 | return a
|
409 | 411 |
|
410 | 412 |
|
| 413 | +# Asynchronous Iterator Operations ********************************************# |
| 414 | + |
| 415 | +async def aiter(*args): |
| 416 | + """aiter(async_iterable) -> async_iterator |
| 417 | + aiter(async_callable, sentinel) -> async_iterator |
| 418 | +
|
| 419 | + An async version of the iter() builtin. |
| 420 | + """ |
| 421 | + lenargs = len(args) |
| 422 | + if lenargs != 1 and lenargs != 2: |
| 423 | + raise TypeError(f'aiter expected 1 or 2 arguments, got {lenargs}') |
| 424 | + if lenargs == 1: |
| 425 | + obj, = args |
| 426 | + if not isinstance(obj, AsyncIterable): |
| 427 | + raise TypeError(f'aiter expected an AsyncIterable, got {type(obj)}') |
| 428 | + async for i in obj.__aiter__(): |
| 429 | + yield i |
| 430 | + return |
| 431 | + # lenargs == 2 |
| 432 | + async_callable, sentinel = args |
| 433 | + while True: |
| 434 | + value = await async_callable() |
| 435 | + if value == sentinel: |
| 436 | + break |
| 437 | + yield value |
| 438 | + |
| 439 | + |
| 440 | +async def anext(*args): |
| 441 | + """anext(async_iterator[, default]) |
| 442 | +
|
| 443 | + Return the next item from the async iterator. |
| 444 | + If default is given and the iterator is exhausted, |
| 445 | + it is returned instead of raising StopAsyncIteration. |
| 446 | + """ |
| 447 | + lenargs = len(args) |
| 448 | + if lenargs != 1 and lenargs != 2: |
| 449 | + raise TypeError(f'anext expected 1 or 2 arguments, got {lenargs}') |
| 450 | + ait = args[0] |
| 451 | + if not isinstance(ait, AsyncIterator): |
| 452 | + raise TypeError(f'anext expected an AsyncIterable, got {type(ait)}') |
| 453 | + anxt = ait.__anext__ |
| 454 | + try: |
| 455 | + return await anxt() |
| 456 | + except StopAsyncIteration: |
| 457 | + if lenargs == 1: |
| 458 | + raise |
| 459 | + return args[1] # default |
| 460 | + |
| 461 | + |
411 | 462 | try:
|
412 | 463 | from _operator import *
|
413 | 464 | except ImportError:
|
|
0 commit comments