mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 12:59:46 +00:00
Reduce __enter__/__exit__ boilerplate for sync ctx managers
This commit is contained in:
@@ -102,6 +102,25 @@ async def _cancel(log, **tasks):
|
||||
except Exception:
|
||||
log.exception('Unhandled exception from %s after cancel', name)
|
||||
|
||||
|
||||
def _sync_enter(self):
|
||||
"""
|
||||
Helps to cut boilerplate on async context
|
||||
managers that offer synchronous variants.
|
||||
"""
|
||||
if self._client.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__())
|
||||
|
||||
|
||||
def _sync_exit(self, *args):
|
||||
return self._client.loop.run_until_complete(self.__aexit__(*args))
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region Cryptographic related utils
|
||||
|
Reference in New Issue
Block a user