From f4b9c9d6d40673132248d037a4a289b3ff13f384 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 14 Aug 2018 19:14:13 +0200 Subject: [PATCH] Remove critical code from assert statements --- telethon/events/raw.py | 8 ++++++-- telethon/network/authenticator.py | 4 +++- telethon/network/mtprotoplainsender.py | 9 ++++++--- telethon/tl/core/gzippacked.py | 3 ++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/telethon/events/raw.py b/telethon/events/raw.py index 229fdd53..67232292 100644 --- a/telethon/events/raw.py +++ b/telethon/events/raw.py @@ -16,10 +16,14 @@ class Raw(EventBuilder): if not types: self.types = None elif not utils.is_list_like(types): - assert isinstance(types, type) + if not isinstance(types, type): + raise TypeError('Invalid input type given %s', types) + self.types = types else: - assert all(isinstance(x, type) for x in types) + if not all(isinstance(x, type) for x in types): + raise TypeError('Invalid input types given %s', types) + self.types = tuple(types) async def resolve(self, client): diff --git a/telethon/network/authenticator.py b/telethon/network/authenticator.py index d79ae7e2..68006abc 100644 --- a/telethon/network/authenticator.py +++ b/telethon/network/authenticator.py @@ -163,7 +163,9 @@ async def do_authentication(sender): if dh_hash != new_nonce_hash: raise SecurityError('Step 3 invalid new nonce hash') - assert isinstance(dh_gen, DhGenOk), 'Step 3.2 answer was %s' % dh_gen + if not isinstance(dh_gen, DhGenOk): + raise AssertionError('Step 3.2 answer was %s' % dh_gen) + return auth_key, time_offset diff --git a/telethon/network/mtprotoplainsender.py b/telethon/network/mtprotoplainsender.py index 3f789f76..942c5e02 100644 --- a/telethon/network/mtprotoplainsender.py +++ b/telethon/network/mtprotoplainsender.py @@ -39,15 +39,18 @@ class MTProtoPlainSender: raise BrokenAuthKeyError() with BinaryReader(body) as reader: - assert reader.read_long() == 0, 'Bad auth_key_id' # auth_key_id + auth_key_id = reader.read_long() + assert auth_key_id == 0, 'Bad auth_key_id' - assert reader.read_long() != 0, 'Bad msg_id' # msg_id + msg_id = reader.read_long() + assert msg_id != 0, 'Bad msg_id' # ^ We should make sure that the read ``msg_id`` is greater # than our own ``msg_id``. However, under some circumstances # (bad system clock/working behind proxies) this seems to not # be the case, which would cause endless assertion errors. - assert reader.read_int() > 0, 'Bad length' # length + length = reader.read_int() + assert length > 0, 'Bad length' # We could read length bytes and use those in a new reader to read # the next TLObject without including the padding, but since the # reader isn't used for anything else after this, it's unnecessary. diff --git a/telethon/tl/core/gzippacked.py b/telethon/tl/core/gzippacked.py index 33c28fe5..4864907e 100644 --- a/telethon/tl/core/gzippacked.py +++ b/telethon/tl/core/gzippacked.py @@ -31,7 +31,8 @@ class GzipPacked(TLObject): @staticmethod def read(reader): - assert reader.read_int(signed=False) == GzipPacked.CONSTRUCTOR_ID + constructor = reader.read_int(signed=False) + assert constructor == GzipPacked.CONSTRUCTOR_ID return gzip.decompress(reader.tgread_bytes()) @classmethod