From 32786d16fdfb33d9feb297e208ca94a68fffa332 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 26 Jun 2018 11:26:01 +0200 Subject: [PATCH] Support using the client in with blocks --- telethon/client/auth.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/telethon/client/auth.py b/telethon/client/auth.py index ca8d24f5..15e8d4ac 100644 --- a/telethon/client/auth.py +++ b/telethon/client/auth.py @@ -1,8 +1,7 @@ import getpass import hashlib -import sys - import os +import sys from .messageparse import MessageParseMethods from .users import UserMethods @@ -459,3 +458,19 @@ class AuthMethods(MessageParseMethods, UserMethods): )) # endregion + + # region with blocks + + def __enter__(self): + return self.start() + + async def __aenter__(self): + return await self.start() + + def __exit__(self, *args): + self.disconnect() + + async def __aexit__(self, *args): + await self.disconnect() + + # endregion