From 9c0a22ddd70fc78831dc13f016b7d5984d44feb0 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 20 Jun 2018 20:18:16 +0200 Subject: [PATCH] Fix Python 3.5 compatibility --- telethon/client/uploads.py | 17 ++++++------ telethon/client/users.py | 33 +++++++++++++---------- telethon_generator/generators/tlobject.py | 18 +++++++++---- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index c52fdae5..cdac46c3 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -129,15 +129,15 @@ class UploadMethods(MessageParseMethods, UserMethods): ) images = images[10:] - result.extend( - await self.send_file( + for x in documents: + result.append(await self.send_file( entity, x, allow_cache=allow_cache, caption=caption, force_document=force_document, progress_callback=progress_callback, reply_to=reply_to, attributes=attributes, thumb=thumb, voice_note=voice_note, video_note=video_note, **kwargs - ) for x in documents - ) + )) + return result entity = await self.get_input_entity(entity) @@ -186,10 +186,11 @@ class UploadMethods(MessageParseMethods, UserMethods): entity = await self.get_input_entity(entity) if not utils.is_list_like(caption): caption = (caption,) - captions = [ - await self._parse_message_text(caption or '', parse_mode) - for caption in reversed(caption) # Pop from the end (so reverse) - ] + + captions = [] + for c in reversed(caption): # Pop from the end (so reverse) + captions.append(await self._parse_message_text(c or '', parse_mode)) + reply_to = utils.get_message_id(reply_to) # Need to upload the media first, but only if they're not cached yet diff --git a/telethon/client/users.py b/telethon/client/users.py index bd85e0c5..e53760e4 100644 --- a/telethon/client/users.py +++ b/telethon/client/users.py @@ -130,10 +130,13 @@ class UserMethods(TelegramBaseClient): # input users (get users), input chat (get chats) and # input channels (get channels) to get the most entities # in the less amount of calls possible. - inputs = [ - x if isinstance(x, str) else await self.get_input_entity(x) - for x in entity - ] + inputs = [] + for x in entity: + if isinstance(x, str): + inputs.append(x) + else: + inputs.append(await self.get_input_entity(x)) + users = [x for x in inputs if isinstance(x, (types.InputPeerUser, types.InputPeerSelf))] chats = [x.chat_id for x in inputs @@ -164,16 +167,18 @@ class UserMethods(TelegramBaseClient): # chats and channels list from before. While this would reduce # the amount of ResolveUsername calls, it would fail to catch # username changes. - result = [ - await self._get_entity_from_string(x) if isinstance(x, str) - else ( - id_entity[utils.get_peer_id(x)] - if not isinstance(x, types.InputPeerSelf) - else next(u for u in id_entity.values() - if isinstance(u, types.User) and u.is_self) - ) - for x in inputs - ] + result = [] + for x in inputs: + if isinstance(x, str): + result.append(await self._get_entity_from_string(x)) + elif not isinstance(x, types.InputPeerSelf): + result.append(id_entity[utils.get_peer_id(x)]) + else: + result.append(next( + u for u in id_entity.values() + if isinstance(u, types.User) and u.is_self + )) + return result[0] if single else result async def get_input_entity(self, peer): diff --git a/telethon_generator/generators/tlobject.py b/telethon_generator/generators/tlobject.py index ac8945b9..111b0c75 100644 --- a/telethon_generator/generators/tlobject.py +++ b/telethon_generator/generators/tlobject.py @@ -238,14 +238,22 @@ def _write_resolve(tlobject, builder): ac = AUTO_CASTS.get(arg.type, None) if not ac: continue + + if arg.is_flag: + builder.writeln('if self.{}:', arg.name) + if arg.is_vector: - builder.write('self.{0} = [{1} for _x in self.{0}]', - arg.name, ac.format('_x')) + builder.writeln('_tmp = []') + builder.writeln('for _x in self.{0}:', arg.name) + builder.writeln('_tmp.append({})', ac.format('_x')) + builder.end_block() + builder.writeln('self.{} = _tmp', arg.name) else: - builder.write('self.{} = {}', arg.name, + builder.writeln('self.{} = {}', arg.name, ac.format('self.' + arg.name)) - builder.writeln(' if self.{} else None'.format(arg.name) - if arg.is_flag else '') + + if arg.is_flag: + builder.end_block() builder.end_block()