From cf6686ff42819885141ef6b3bf22466c915b3056 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 12 Oct 2018 22:00:02 +0200 Subject: [PATCH] Revert async sessions from 3dd8b7c (breaks sync properties) Such as ChatGetter.input_chat and SenderGetter.input_sender which rely on the fact that access to the session file will be synchronous. --- telethon/client/auth.py | 2 +- telethon/client/downloads.py | 4 ++-- telethon/client/telegrambaseclient.py | 22 +++++++++++++++------- telethon/client/updates.py | 8 ++++---- telethon/client/uploads.py | 6 +++--- telethon/client/users.py | 8 ++++---- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/telethon/client/auth.py b/telethon/client/auth.py index 5650cd97..bbb1d57b 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -399,7 +399,7 @@ class AuthMethods(MessageParseMethods, UserMethods): self._self_input_peer = None self._state.pts = -1 await self.disconnect() - await self.session.delete() + self.session.delete() return True async def edit_2fa( diff --git a/telethon/client/downloads.py b/telethon/client/downloads.py index f8ab4ffc..4c0e1239 100644 --- a/telethon/client/downloads.py +++ b/telethon/client/downloads.py @@ -223,9 +223,9 @@ class DownloadMethods(UserMethods): config = await self(functions.help.GetConfigRequest()) for option in config.dc_options: if option.ip_address == self.session.server_address: - await self.session.set_dc( + self.session.set_dc( option.id, option.ip_address, option.port) - await self.session.save() + self.session.save() break # TODO Figure out why the session may have the wrong DC ID diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index cecd6236..60f6269f 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -197,7 +197,15 @@ class TelegramBaseClient(abc.ABC): ) self.flood_sleep_threshold = flood_sleep_threshold - self.session = AsyncClassWrapper(session) + + # TODO Figure out how to use AsyncClassWrapper(session) + # The problem is that ChatGetter and SenderGetter rely + # on synchronous calls to session.get_entity precisely + # to avoid network access and the need for await. + # + # With asynchronous sessions, it would need await, + # and defeats the purpose of properties. + self.session = session self.api_id = int(api_id) self.api_hash = api_hash @@ -325,8 +333,8 @@ class TelegramBaseClient(abc.ABC): await self._disconnect() if getattr(self, 'session', None): if getattr(self, '_state', None): - await self.session.set_update_state(0, self._state) - await self.session.close() + self.session.set_update_state(0, self._state) + self.session.close() async def _disconnect(self): """ @@ -364,11 +372,11 @@ class TelegramBaseClient(abc.ABC): __log__.info('Reconnecting to new data center %s', new_dc) dc = await self._get_dc(new_dc) - await self.session.set_dc(dc.id, dc.ip_address, dc.port) + self.session.set_dc(dc.id, dc.ip_address, dc.port) # auth_key's are associated with a server, which has now changed # so it's not valid anymore. Set to None to force recreating it. self.session.auth_key = None - await self.session.save() + self.session.save() await self._disconnect() return await self.connect() @@ -378,7 +386,7 @@ class TelegramBaseClient(abc.ABC): new authorization key. This means we are not authorized. """ self.session.auth_key = auth_key - await self.session.save() + self.session.save() # endregion @@ -466,7 +474,7 @@ class TelegramBaseClient(abc.ABC): session = self._exported_sessions.get(cdn_redirect.dc_id) if not session: dc = await self._get_dc(cdn_redirect.dc_id, cdn=True) - session = await self.session.clone() + session = self.session.clone() await session.set_dc(dc.id, dc.ip_address, dc.port) self._exported_sessions[cdn_redirect.dc_id] = session diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 8bac5c9c..1c212d21 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -137,7 +137,7 @@ class UpdateMethods(UserMethods): This can also be used to forcibly fetch new updates if there are any. """ - state = await self.session.get_update_state(0) + state = self.session.get_update_state(0) if not state or not state.pts: state = await self(functions.updates.GetStateRequest()) @@ -172,7 +172,7 @@ class UpdateMethods(UserMethods): state.pts = d.pts break finally: - await self.session.set_update_state(0, state) + self.session.set_update_state(0, state) self.session.catching_up = False # endregion @@ -180,7 +180,7 @@ class UpdateMethods(UserMethods): # region Private methods async def _handle_update(self, update): - await self.session.process_entities(update) + self.session.process_entities(update) if isinstance(update, (types.Updates, types.UpdatesCombined)): entities = {utils.get_peer_id(x): x for x in itertools.chain(update.users, update.chats)} @@ -236,7 +236,7 @@ class UpdateMethods(UserMethods): # inserted because this is a rather expensive operation # (default's sqlite3 takes ~0.1s to commit changes). Do # it every minute instead. No-op if there's nothing new. - await self.session.save() + self.session.save() # We need to send some content-related request at least hourly # for Telegram to keep delivering updates, otherwise they will diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 107c2fdc..4b6e3dbe 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -213,7 +213,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): entity, media=types.InputMediaUploadedPhoto(fh) )) input_photo = utils.get_input_photo(r.photo) - await self.session.cache_file(fh.md5, fh.size, input_photo) + self.session.cache_file(fh.md5, fh.size, input_photo) fh = input_photo if captions: @@ -328,7 +328,7 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): file = stream.read() hash_md5.update(file) if use_cache: - cached = await self.session.get_file( + cached = self.session.get_file( hash_md5.digest(), file_size, cls=use_cache ) if cached: @@ -459,6 +459,6 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): to_cache = utils.get_input_photo(msg.media.photo) else: to_cache = utils.get_input_document(msg.media.document) - await self.session.cache_file(md5, size, to_cache) + self.session.cache_file(md5, size, to_cache) # endregion diff --git a/telethon/client/users.py b/telethon/client/users.py index cc92529a..ccbdc5e7 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -48,7 +48,7 @@ class UserMethods(TelegramBaseClient): exceptions.append(e) results.append(None) continue - await self.session.process_entities(result) + self.session.process_entities(result) exceptions.append(None) results.append(result) request_index += 1 @@ -58,7 +58,7 @@ class UserMethods(TelegramBaseClient): return results else: result = await future - await self.session.process_entities(result) + self.session.process_entities(result) return result except (errors.ServerError, errors.RpcCallFailError) as e: __log__.warning('Telegram is having internal issues %s: %s', @@ -288,7 +288,7 @@ class UserMethods(TelegramBaseClient): try: # First try to get the entity from cache, otherwise figure it out - return await self.session.get_input_entity(peer) + return self.session.get_input_entity(peer) except ValueError: pass @@ -393,7 +393,7 @@ class UserMethods(TelegramBaseClient): try: # Nobody with this username, maybe it's an exact name/title return await self.get_entity( - await self.session.get_input_entity(string)) + self.session.get_input_entity(string)) except ValueError: pass