Skip to content

Commit a049173

Browse files
directly handle stopiteration
1 parent b730600 commit a049173

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

asyncstdlib/itertools.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,23 +381,24 @@ async def __anext__(self) -> T:
381381
# Check if another peer produced an item while we were waiting for the lock
382382
if not next_node:
383383
await self._extend_buffer(next_node)
384-
if not next_node:
385-
raise StopAsyncIteration()
386384
# for any other TeePeer, the node is already some [value, [...]]
387385
value, self._buffer = next_node # type: ignore
388386
return value
389387

390388
async def _extend_buffer(self, next_node: "_TeeNode[T]") -> None:
391389
"""Extend the buffer by fetching a new item from the iterable"""
392390
try:
391+
# another peer may fill the buffer while we wait here
393392
next_value = await self._iterator.__anext__()
394393
except StopAsyncIteration:
395-
return
396-
# another peer may have filled the buffer while we waited
397-
# seek the last node that needs to be filled
398-
while next_node:
399-
_, next_node = next_node # type: ignore
400-
next_node[:] = next_value, []
394+
# no one else managed to fetch a value either
395+
if not next_node:
396+
raise
397+
else:
398+
# seek the last node that needs to be filled
399+
while next_node:
400+
_, next_node = next_node # type: ignore
401+
next_node[:] = next_value, []
401402

402403
async def aclose(self) -> None:
403404
self._tee_peers.discard(self._tee_idx)

0 commit comments

Comments
 (0)