From 20b82500378e15acd2a810752d0223d7d1a24556 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 13 Apr 2019 10:55:51 +0200 Subject: [PATCH] Fix-up new sync __enter__ not handling the client directly --- telethon/helpers.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/telethon/helpers.py b/telethon/helpers.py index 531cc421..99196ca7 100644 --- a/telethon/helpers.py +++ b/telethon/helpers.py @@ -108,17 +108,27 @@ def _sync_enter(self): Helps to cut boilerplate on async context managers that offer synchronous variants. """ - if self._client.loop.is_running(): + if hasattr(self, 'loop'): + loop = self.loop + else: + loop = self._client.loop + + if loop.is_running(): raise RuntimeError( 'You must use "async with" if the event loop ' 'is running (i.e. you are inside an "async def")' ) - return self._client.loop.run_until_complete(self.__aenter__()) + return loop.run_until_complete(self.__aenter__()) def _sync_exit(self, *args): - return self._client.loop.run_until_complete(self.__aexit__(*args)) + if hasattr(self, 'loop'): + loop = self.loop + else: + loop = self._client.loop + + return loop.run_until_complete(self.__aexit__(*args)) # endregion