Skip to content

Commit 4d933b8

Browse files
Fix TL schema naming conflicts (pyrogram#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>
1 parent 6745c9d commit 4d933b8

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

compiler/api/compiler.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,11 @@ def start(format: bool = False):
416416
write_flags = "\n ".join([
417417
"flags = 0",
418418
"\n ".join(write_flags),
419-
"data.write(Int(flags))\n "
419+
"b.write(Int(flags))\n "
420420
])
421421

422422
write_types += write_flags
423-
read_types += "flags = Int.read(data)\n "
423+
read_types += "flags = Int.read(b)\n "
424424

425425
continue
426426

@@ -433,55 +433,55 @@ def start(format: bool = False):
433433
elif flag_type in CORE_TYPES:
434434
write_types += "\n "
435435
write_types += f"if self.{arg_name} is not None:\n "
436-
write_types += f"data.write({flag_type.title()}(self.{arg_name}))\n "
436+
write_types += f"b.write({flag_type.title()}(self.{arg_name}))\n "
437437

438438
read_types += "\n "
439-
read_types += f"{arg_name} = {flag_type.title()}.read(data) if flags & (1 << {index}) else None"
439+
read_types += f"{arg_name} = {flag_type.title()}.read(b) if flags & (1 << {index}) else None"
440440
elif "vector" in flag_type.lower():
441441
sub_type = arg_type.split("<")[1][:-1]
442442

443443
write_types += "\n "
444444
write_types += f"if self.{arg_name} is not None:\n "
445-
write_types += "data.write(Vector(self.{}{}))\n ".format(
445+
write_types += "b.write(Vector(self.{}{}))\n ".format(
446446
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else ""
447447
)
448448

449449
read_types += "\n "
450-
read_types += "{} = TLObject.read(data{}) if flags & (1 << {}) else []\n ".format(
450+
read_types += "{} = TLObject.read(b{}) if flags & (1 << {}) else []\n ".format(
451451
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else "", index
452452
)
453453
else:
454454
write_types += "\n "
455455
write_types += f"if self.{arg_name} is not None:\n "
456-
write_types += f"data.write(self.{arg_name}.write())\n "
456+
write_types += f"b.write(self.{arg_name}.write())\n "
457457

458458
read_types += "\n "
459-
read_types += f"{arg_name} = TLObject.read(data) if flags & (1 << {index}) else None\n "
459+
read_types += f"{arg_name} = TLObject.read(b) if flags & (1 << {index}) else None\n "
460460
else:
461461
if arg_type in CORE_TYPES:
462462
write_types += "\n "
463-
write_types += f"data.write({arg_type.title()}(self.{arg_name}))\n "
463+
write_types += f"b.write({arg_type.title()}(self.{arg_name}))\n "
464464

465465
read_types += "\n "
466-
read_types += f"{arg_name} = {arg_type.title()}.read(data)\n "
466+
read_types += f"{arg_name} = {arg_type.title()}.read(b)\n "
467467
elif "vector" in arg_type.lower():
468468
sub_type = arg_type.split("<")[1][:-1]
469469

470470
write_types += "\n "
471-
write_types += "data.write(Vector(self.{}{}))\n ".format(
471+
write_types += "b.write(Vector(self.{}{}))\n ".format(
472472
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else ""
473473
)
474474

475475
read_types += "\n "
476-
read_types += "{} = TLObject.read(data{})\n ".format(
476+
read_types += "{} = TLObject.read(b{})\n ".format(
477477
arg_name, f", {sub_type.title()}" if sub_type in CORE_TYPES else ""
478478
)
479479
else:
480480
write_types += "\n "
481-
write_types += f"data.write(self.{arg_name}.write())\n "
481+
write_types += f"b.write(self.{arg_name}.write())\n "
482482

483483
read_types += "\n "
484-
read_types += f"{arg_name} = TLObject.read(data)\n "
484+
read_types += f"{arg_name} = TLObject.read(b)\n "
485485

486486
slots = ", ".join([f'"{i[0]}"' for i in sorted_args])
487487
return_arguments = ", ".join([f"{i[0]}={i[0]}" for i in sorted_args])

compiler/api/template/combinator.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class {name}(TLObject): # type: ignore
2323
{fields}
2424

2525
@staticmethod
26-
def read(data: BytesIO, *args: Any) -> "{name}":
26+
def read(b: BytesIO, *args: Any) -> "{name}":
2727
{read_types}
2828
return {name}({return_arguments})
2929

3030
def write(self) -> bytes:
31-
data = BytesIO()
32-
data.write(Int(self.ID, False))
31+
b = BytesIO()
32+
b.write(Int(self.ID, False))
3333

3434
{write_types}
35-
return data.getvalue()
35+
return b.getvalue()

pyrogram/raw/core/tl_object.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class TLObject:
2929
QUALNAME = "Base"
3030

3131
@classmethod
32-
def read(cls, data: BytesIO, *args: Any) -> Any:
33-
return cast(TLObject, objects[int.from_bytes(data.read(4), "little")]).read(data, *args)
32+
def read(cls, b: BytesIO, *args: Any) -> Any:
33+
return cast(TLObject, objects[int.from_bytes(b.read(4), "little")]).read(b, *args)
3434

3535
def write(self, *args: Any) -> bytes:
3636
pass

pyrogram/types/bots_and_keyboards/callback_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async def _parse(client, callback_query, users) -> "CallbackQuery":
110110
# ignoring/replacing errors, this way, button clicks will still work.
111111
try:
112112
data = callback_query.data.decode()
113-
except UnicodeDecodeError:
113+
except (UnicodeDecodeError, AttributeError):
114114
data = callback_query.data
115115

116116
return CallbackQuery(

0 commit comments

Comments
 (0)