From 4d933b80f96810b7a652a328dcc74f2645935362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A4=85=E5=96=B5?= Date: Sat, 28 Aug 2021 16:01:12 +0800 Subject: [PATCH] Fix TL schema naming conflicts (#690) * Avoid variable conflicts with Telegram TL schema * Fix game button with no data attached to button * Update combinator.txt * Update compiler.py * Update tl_object.py Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com> --- compiler/api/compiler.py | 28 +++++++++---------- compiler/api/template/combinator.txt | 8 +++--- pyrogram/raw/core/tl_object.py | 4 +-- .../bots_and_keyboards/callback_query.py | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index 307a9d49..d79fc801 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -416,11 +416,11 @@ def start(format: bool = False): write_flags = "\n ".join([ "flags = 0", "\n ".join(write_flags), - "data.write(Int(flags))\n " + "b.write(Int(flags))\n " ]) write_types += write_flags - read_types += "flags = Int.read(data)\n " + read_types += "flags = Int.read(b)\n " continue @@ -433,55 +433,55 @@ def start(format: bool = False): elif flag_type in CORE_TYPES: write_types += "\n " write_types += f"if self.{arg_name} is not None:\n " - write_types += f"data.write({flag_type.title()}(self.{arg_name}))\n " + write_types += f"b.write({flag_type.title()}(self.{arg_name}))\n " read_types += "\n " - read_types += f"{arg_name} = {flag_type.title()}.read(data) if flags & (1 << {index}) else None" + read_types += f"{arg_name} = {flag_type.title()}.read(b) if flags & (1 << {index}) else None" elif "vector" in flag_type.lower(): sub_type = arg_type.split("<")[1][:-1] write_types += "\n " write_types += f"if self.{arg_name} is not None:\n " - write_types += "data.write(Vector(self.{}{}))\n ".format( + write_types += "b.write(Vector(self.{}{}))\n ".format( arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else "" ) read_types += "\n " - read_types += "{} = TLObject.read(data{}) if flags & (1 << {}) else []\n ".format( + read_types += "{} = TLObject.read(b{}) if flags & (1 << {}) else []\n ".format( arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else "", index ) else: write_types += "\n " write_types += f"if self.{arg_name} is not None:\n " - write_types += f"data.write(self.{arg_name}.write())\n " + write_types += f"b.write(self.{arg_name}.write())\n " read_types += "\n " - read_types += f"{arg_name} = TLObject.read(data) if flags & (1 << {index}) else None\n " + read_types += f"{arg_name} = TLObject.read(b) if flags & (1 << {index}) else None\n " else: if arg_type in CORE_TYPES: write_types += "\n " - write_types += f"data.write({arg_type.title()}(self.{arg_name}))\n " + write_types += f"b.write({arg_type.title()}(self.{arg_name}))\n " read_types += "\n " - read_types += f"{arg_name} = {arg_type.title()}.read(data)\n " + read_types += f"{arg_name} = {arg_type.title()}.read(b)\n " elif "vector" in arg_type.lower(): sub_type = arg_type.split("<")[1][:-1] write_types += "\n " - write_types += "data.write(Vector(self.{}{}))\n ".format( + write_types += "b.write(Vector(self.{}{}))\n ".format( arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else "" ) read_types += "\n " - read_types += "{} = TLObject.read(data{})\n ".format( + read_types += "{} = TLObject.read(b{})\n ".format( arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else "" ) else: write_types += "\n " - write_types += f"data.write(self.{arg_name}.write())\n " + write_types += f"b.write(self.{arg_name}.write())\n " read_types += "\n " - read_types += f"{arg_name} = TLObject.read(data)\n " + read_types += f"{arg_name} = TLObject.read(b)\n " slots = ", ".join([f'"{i[0]}"' for i in sorted_args]) return_arguments = ", ".join([f"{i[0]}={i[0]}" for i in sorted_args]) diff --git a/compiler/api/template/combinator.txt b/compiler/api/template/combinator.txt index 318052a7..e0275dd1 100644 --- a/compiler/api/template/combinator.txt +++ b/compiler/api/template/combinator.txt @@ -23,13 +23,13 @@ class {name}(TLObject): # type: ignore {fields} @staticmethod - def read(data: BytesIO, *args: Any) -> "{name}": + def read(b: BytesIO, *args: Any) -> "{name}": {read_types} return {name}({return_arguments}) def write(self) -> bytes: - data = BytesIO() - data.write(Int(self.ID, False)) + b = BytesIO() + b.write(Int(self.ID, False)) {write_types} - return data.getvalue() + return b.getvalue() diff --git a/pyrogram/raw/core/tl_object.py b/pyrogram/raw/core/tl_object.py index 3b87d72c..f0dd596a 100644 --- a/pyrogram/raw/core/tl_object.py +++ b/pyrogram/raw/core/tl_object.py @@ -29,8 +29,8 @@ class TLObject: QUALNAME = "Base" @classmethod - def read(cls, data: BytesIO, *args: Any) -> Any: - return cast(TLObject, objects[int.from_bytes(data.read(4), "little")]).read(data, *args) + def read(cls, b: BytesIO, *args: Any) -> Any: + return cast(TLObject, objects[int.from_bytes(b.read(4), "little")]).read(b, *args) def write(self, *args: Any) -> bytes: pass diff --git a/pyrogram/types/bots_and_keyboards/callback_query.py b/pyrogram/types/bots_and_keyboards/callback_query.py index 11e749d1..8887594e 100644 --- a/pyrogram/types/bots_and_keyboards/callback_query.py +++ b/pyrogram/types/bots_and_keyboards/callback_query.py @@ -110,7 +110,7 @@ class CallbackQuery(Object, Update): # ignoring/replacing errors, this way, button clicks will still work. try: data = callback_query.data.decode() - except UnicodeDecodeError: + except (UnicodeDecodeError, AttributeError): data = callback_query.data return CallbackQuery(