|
5 | 5 | copied into the ``builtins`` module. |
6 | 6 | """ |
7 | 7 |
|
8 | | -__all__ = ['anext'] |
| 8 | +__all__ = ['aiter', 'anext'] |
9 | 9 |
|
10 | 10 | _NOT_GIVEN = sentinel("_NOT_GIVEN") |
11 | 11 |
|
12 | 12 |
|
| 13 | +def aiter(object, /, stop_value=_NOT_GIVEN, *, stop_exception=_NOT_GIVEN): |
| 14 | + """Return an AsyncIterator for an AsyncIterable object. |
| 15 | +
|
| 16 | + In the second form, the callable is called and its result is awaited |
| 17 | + until it returns the stop value or raises the specified exception. |
| 18 | + """ |
| 19 | + if stop_value is _NOT_GIVEN and stop_exception is _NOT_GIVEN: |
| 20 | + cls = type(object) |
| 21 | + try: |
| 22 | + # Looked up on the type, like the C slot am_aiter. |
| 23 | + aiter_method = cls.__aiter__ |
| 24 | + except AttributeError: |
| 25 | + raise TypeError( |
| 26 | + f"'{cls.__name__}' object is not an async iterable" |
| 27 | + ) from None |
| 28 | + iterator = aiter_method(object) |
| 29 | + if not hasattr(type(iterator), '__anext__'): |
| 30 | + raise TypeError( |
| 31 | + f"{cls.__name__}.__aiter__() must return an async iterator, " |
| 32 | + f"not {type(iterator).__name__}" |
| 33 | + ) |
| 34 | + return iterator |
| 35 | + |
| 36 | + if not callable(object): |
| 37 | + raise TypeError("aiter(): the first argument must be callable") |
| 38 | + if stop_exception is _NOT_GIVEN: |
| 39 | + stop_exception = StopAsyncIteration |
| 40 | + else: |
| 41 | + # Like _PyEval_CheckExceptTypeValid(): an exception class or a tuple |
| 42 | + # of exception classes. |
| 43 | + if isinstance(stop_exception, tuple): |
| 44 | + items = stop_exception |
| 45 | + else: |
| 46 | + items = (stop_exception,) |
| 47 | + for item in items: |
| 48 | + if not (isinstance(item, type) and issubclass(item, BaseException)): |
| 49 | + raise TypeError( |
| 50 | + "catching classes that do not inherit from BaseException " |
| 51 | + "is not allowed" |
| 52 | + ) |
| 53 | + return async_callable_iterator(object, stop_value, stop_exception) |
| 54 | + |
| 55 | + |
| 56 | +class async_callable_iterator: |
| 57 | + """The asynchronous counterpart of callable_iterator. |
| 58 | +
|
| 59 | + The callable is called and its result is awaited for every |
| 60 | + __anext__(). |
| 61 | + """ |
| 62 | + |
| 63 | + __slots__ = ('_callable', '_stop_value', '_stop_exception') |
| 64 | + __module__ = 'builtins' |
| 65 | + |
| 66 | + def __init__(self, callable, stop_value, stop_exception): |
| 67 | + # _callable is set to None when the iterator is exhausted. |
| 68 | + self._callable = callable |
| 69 | + self._stop_value = stop_value |
| 70 | + self._stop_exception = stop_exception |
| 71 | + |
| 72 | + def __aiter__(self): |
| 73 | + return self |
| 74 | + |
| 75 | + async def __anext__(self): |
| 76 | + callable = self._callable |
| 77 | + if callable is None: |
| 78 | + raise StopAsyncIteration |
| 79 | + try: |
| 80 | + value = await callable() |
| 81 | + except self._stop_exception: |
| 82 | + self._exhaust() |
| 83 | + raise StopAsyncIteration from None |
| 84 | + except (StopIteration, StopAsyncIteration) as exc: |
| 85 | + raise RuntimeError( |
| 86 | + f"callable raised {type(exc).__name__}") from exc |
| 87 | + stop_value = self._stop_value |
| 88 | + if stop_value is not _NOT_GIVEN and stop_value == value: |
| 89 | + self._exhaust() |
| 90 | + raise StopAsyncIteration |
| 91 | + return value |
| 92 | + |
| 93 | + def _exhaust(self): |
| 94 | + self._callable = None |
| 95 | + self._stop_value = _NOT_GIVEN |
| 96 | + |
| 97 | + |
13 | 98 | def anext(async_iterator, default=_NOT_GIVEN, /): |
14 | 99 | """Return the next item from the async iterator. |
15 | 100 |
|
|
0 commit comments