From 6799295115513d5f51c807fabac16dbddc7f8145 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 2 Mar 2019 21:17:36 +0100 Subject: [PATCH] Fix iter_participants in non-channels --- telethon/client/chats.py | 11 +++++++---- telethon/requestiter.py | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/telethon/client/chats.py b/telethon/client/chats.py index fd8cce14..9917f70f 100644 --- a/telethon/client/chats.py +++ b/telethon/client/chats.py @@ -35,6 +35,9 @@ class _ParticipantsIter(RequestIter): else: self.filter_entity = lambda ent: True + # Only used for channels, but we should always set the attribute + self.requests = [] + if isinstance(entity, types.InputPeerChannel): self.total = (await self.client( functions.channels.GetFullChannelRequest(entity) @@ -45,21 +48,21 @@ class _ParticipantsIter(RequestIter): self.seen = set() if aggressive and not filter: - self.requests = [functions.channels.GetParticipantsRequest( + self.requests.extend(functions.channels.GetParticipantsRequest( channel=entity, filter=types.ChannelParticipantsSearch(x), offset=0, limit=_MAX_PARTICIPANTS_CHUNK_SIZE, hash=0 - ) for x in (search or string.ascii_lowercase)] + ) for x in (search or string.ascii_lowercase)) else: - self.requests = [functions.channels.GetParticipantsRequest( + self.requests.append(functions.channels.GetParticipantsRequest( channel=entity, filter=filter or types.ChannelParticipantsSearch(search), offset=0, limit=_MAX_PARTICIPANTS_CHUNK_SIZE, hash=0 - )] + )) elif isinstance(entity, types.InputPeerChat): full = await self.client( diff --git a/telethon/requestiter.py b/telethon/requestiter.py index ff7bdcf0..6c693303 100644 --- a/telethon/requestiter.py +++ b/telethon/requestiter.py @@ -47,13 +47,16 @@ class RequestIter(abc.ABC): This method may ``raise StopAsyncIteration`` if it cannot continue. - This method may actually fill the initial buffer if it needs to. + This method may actually fill the initial buffer if it needs to, + and similarly to `_load_next_chunk`, ``return True`` to indicate + that this is the last iteration (just the initial load). """ async def __anext__(self): if self.buffer is None: self.buffer = [] - await self._init(**self.kwargs) + if await self._init(**self.kwargs): + self.left = len(self.buffer) if self.left <= 0: # <= 0 because subclasses may change it raise StopAsyncIteration