From 4258ce2bc8455f37f230f59bf1d08f8834b3f8bc Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 16 Feb 2022 11:24:20 +0100 Subject: [PATCH] Make is_connected a property This is consistent with the rest of is_ properties --- readthedocs/misc/v2-migration-guide.rst | 2 ++ telethon/_client/auth.py | 2 +- telethon/_client/telegrambaseclient.py | 41 ++++++++++++------------- telethon/_client/telegramclient.py | 6 ++-- telethon/_client/updates.py | 2 +- telethon_examples/gui.py | 2 +- 6 files changed, 29 insertions(+), 26 deletions(-) diff --git a/readthedocs/misc/v2-migration-guide.rst b/readthedocs/misc/v2-migration-guide.rst index 09618508..12ae66ba 100644 --- a/readthedocs/misc/v2-migration-guide.rst +++ b/readthedocs/misc/v2-migration-guide.rst @@ -976,3 +976,5 @@ Now the URL is returned. You can still use ``webbrowser.open`` to get the old be todo update send_message and send_file docs (well review all functions) album overhaul. use a list of Message instead. + +is_connected is now a property (consistent with the rest of ``is_`` properties) diff --git a/telethon/_client/auth.py b/telethon/_client/auth.py index a4942a64..67e6382e 100644 --- a/telethon/_client/auth.py +++ b/telethon/_client/auth.py @@ -71,7 +71,7 @@ def start( async def _start( self: 'TelegramClient', phone, password, bot_token, code_callback, first_name, last_name, max_attempts): - if not self.is_connected(): + if not self.is_connected: await self.connect() # Rather than using `is_user_authorized`, use `get_me`. While this is diff --git a/telethon/_client/telegrambaseclient.py b/telethon/_client/telegrambaseclient.py index 09b1db51..aaaa9a7f 100644 --- a/telethon/_client/telegrambaseclient.py +++ b/telethon/_client/telegrambaseclient.py @@ -308,12 +308,29 @@ async def connect(self: 'TelegramClient') -> None: self._updates_handle = asyncio.create_task(self._update_loop()) + def is_connected(self: 'TelegramClient') -> bool: - sender = getattr(self, '_sender', None) - return sender and sender.is_connected() + return self._sender.is_connected() + async def disconnect(self: 'TelegramClient'): - return await _disconnect_coro(self) + await _disconnect(self) + + # Also clean-up all exported senders because we're done with them + async with self._borrow_sender_lock: + for state, sender in self._borrowed_senders.values(): + # Note that we're not checking for `state.should_disconnect()`. + # If the user wants to disconnect the client, ALL connections + # to Telegram (including exported senders) should be closed. + # + # Disconnect should never raise, so there's no try/except. + await sender.disconnect() + # Can't use `mark_disconnected` because it may be borrowed. + state._connected = False + + # If any was borrowed + self._borrowed_senders.clear() + def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]): init_proxy = None @@ -333,24 +350,6 @@ def set_proxy(self: 'TelegramClient', proxy: typing.Union[tuple, dict]): else: connection._proxy = proxy -async def _disconnect_coro(self: 'TelegramClient'): - await _disconnect(self) - - # Also clean-up all exported senders because we're done with them - async with self._borrow_sender_lock: - for state, sender in self._borrowed_senders.values(): - # Note that we're not checking for `state.should_disconnect()`. - # If the user wants to disconnect the client, ALL connections - # to Telegram (including exported senders) should be closed. - # - # Disconnect should never raise, so there's no try/except. - await sender.disconnect() - # Can't use `mark_disconnected` because it may be borrowed. - state._connected = False - - # If any was borrowed - self._borrowed_senders.clear() - async def _disconnect(self: 'TelegramClient'): """ diff --git a/telethon/_client/telegramclient.py b/telethon/_client/telegramclient.py index 23babf83..eca8ba84 100644 --- a/telethon/_client/telegramclient.py +++ b/telethon/_client/telegramclient.py @@ -2637,7 +2637,7 @@ class TelegramClient: print('Failed to connect') """ - @forward_call(telegrambaseclient.is_connected) + @property def is_connected(self: 'TelegramClient') -> bool: """ Returns `True` if the user has connected. @@ -2647,9 +2647,11 @@ class TelegramClient: Example .. code-block:: python - while client.is_connected(): + # This is a silly example - run_until_disconnected is often better suited + while client.is_connected: await asyncio.sleep(1) """ + return telegrambaseclient.is_connected(self) @forward_call(telegrambaseclient.disconnect) def disconnect(self: 'TelegramClient'): diff --git a/telethon/_client/updates.py b/telethon/_client/updates.py index 32375dd6..91e0b05e 100644 --- a/telethon/_client/updates.py +++ b/telethon/_client/updates.py @@ -168,7 +168,7 @@ async def catch_up(self: 'TelegramClient'): async def _update_loop(self: 'TelegramClient'): try: updates_to_dispatch = deque() - while self.is_connected(): + while self.is_connected: if updates_to_dispatch: await _dispatch(self, *updates_to_dispatch.popleft()) continue diff --git a/telethon_examples/gui.py b/telethon_examples/gui.py index ea6d248a..78dcfb34 100644 --- a/telethon_examples/gui.py +++ b/telethon_examples/gui.py @@ -228,7 +228,7 @@ class App(tkinter.Tk): """ Sends a message. Does nothing if the client is not connected. """ - if not self.cl.is_connected(): + if not self.cl.is_connected: return # The user needs to configure a chat where the message should be sent.