From d02d0e2d5e4fbaf897ac7e5bab57291ca3448ebf Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 27 Feb 2019 13:01:04 +0100 Subject: [PATCH] Handle negative limits gracefully in async generators We rely on >= 0 for setting the batch size to use (which must be valid), so it makes sense to make negative limits equal 0. This is similar to how asyncio.sleep(negative) sleeps 0 seconds, despite the fact that time.sleep(negative) fails. --- telethon/client/chats.py | 2 +- telethon/client/dialogs.py | 2 +- telethon/client/messages.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/telethon/client/chats.py b/telethon/client/chats.py index b310652d..cb5263fb 100644 --- a/telethon/client/chats.py +++ b/telethon/client/chats.py @@ -37,7 +37,7 @@ class _ParticipantsIter(RequestIter): functions.channels.GetFullChannelRequest(entity) )).full_chat.participants_count - if self.limit == 0: + if self.limit <= 0: raise StopAsyncIteration self.seen = set() diff --git a/telethon/client/dialogs.py b/telethon/client/dialogs.py index 75fbf255..f9782f4d 100644 --- a/telethon/client/dialogs.py +++ b/telethon/client/dialogs.py @@ -18,7 +18,7 @@ class _DialogsIter(RequestIter): hash=0 ) - if self.limit == 0: + if self.limit <= 0: # Special case, get a single dialog and determine count dialogs = await self.client(self.request) self.total = getattr(dialogs, 'count', len(dialogs.dialogs)) diff --git a/telethon/client/messages.py b/telethon/client/messages.py index c14bddeb..d59236d9 100644 --- a/telethon/client/messages.py +++ b/telethon/client/messages.py @@ -111,7 +111,7 @@ class _MessagesIter(RequestIter): hash=0 ) - if self.limit == 0: + if self.limit <= 0: # No messages, but we still need to know the total message count result = await self.client(self.request) if isinstance(result, types.messages.MessagesNotModified):