Skip to content

Commit

Permalink
address comments from first review
Browse files Browse the repository at this point in the history
  • Loading branch information
jab committed Nov 30, 2020
1 parent 44b2a60 commit 142523b
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,51 +408,52 @@ def ixor(a, b):

# Asynchronous Iterator Operations ********************************************#

async def aiter(*args):

_NOT_PROVIDED = object() # sentinel object to detect when a kwarg was not given


def aiter(obj, sentinel=_NOT_PROVIDED):
"""aiter(async_iterable) -> async_iterator
aiter(async_callable, sentinel) -> async_iterator
An async version of the iter() builtin.
Like the iter() builtin but for async iterables and callables.
"""
lenargs = len(args)
if lenargs != 1 and lenargs != 2:
raise TypeError(f'aiter expected 1 or 2 arguments, got {lenargs}')
if lenargs == 1:
obj, = args
if sentinel is _NOT_PROVIDED:
if not isinstance(obj, AsyncIterable):
raise TypeError(f'aiter expected an AsyncIterable, got {type(obj)}')
async for i in obj.__aiter__():
yield i
return
# lenargs == 2
async_callable, sentinel = args
while True:
value = await async_callable()
if value == sentinel:
break
yield value


async def anext(*args):
if isinstance(obj, AsyncIterator):
return obj
return (i async for i in obj)

if not callable(obj):
raise TypeError(f'aiter expected an async callable, got {type(obj)}')

async def ait():
while True:
value = await obj()
if value == sentinel:
break
yield value

return ait()


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.
"""
lenargs = len(args)
if lenargs != 1 and lenargs != 2:
raise TypeError(f'anext expected 1 or 2 arguments, got {lenargs}')
ait = args[0]
if not isinstance(ait, AsyncIterator):
raise TypeError(f'anext expected an AsyncIterable, got {type(ait)}')
anxt = ait.__anext__
if not isinstance(async_iterator, AsyncIterator):
raise TypeError(f'anext expected an AsyncIterator, got {type(async_iterator)}')
anxt = async_iterator.__anext__
try:
return await anxt()
except StopAsyncIteration:
if lenargs == 1:
if default is _NOT_PROVIDED:
raise
return args[1] # default
return default


try:
Expand Down

0 comments on commit 142523b

Please sign in to comment.