Skip to content

Commit c7b1d6f

Browse files
committed
Take into account that flags:# could be not always the first argument
For instance, in Layer 91, Poll's flags:# is at the second position. This mess also happened in the past (thanks tg devs) and eventually will be fixed again with the next Layer update, but from now on Pyrogram will be able to correctly generate code even in such cases.
1 parent a50dba2 commit c7b1d6f

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

compiler/api/compiler.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
SECTION_RE = re.compile(r"---(\w+)---")
2727
LAYER_RE = re.compile(r"//\sLAYER\s(\d+)")
2828
COMBINATOR_RE = re.compile(r"^([\w.]+)#([0-9a-f]+)\s(?:.*)=\s([\w<>.]+);(?: // Docs: (.+))?$", re.MULTILINE)
29-
ARGS_RE = re.compile("[^{](\w+):([\w?!.<>]+)")
29+
ARGS_RE = re.compile("[^{](\w+):([\w?!.<>#]+)")
3030
FLAGS_RE = re.compile(r"flags\.(\d+)\?")
3131
FLAGS_RE_2 = re.compile(r"flags\.(\d+)\?([\w<>.]+)")
3232
FLAGS_RE_3 = re.compile(r"flags:#")
@@ -288,17 +288,20 @@ def start():
288288
sorted_args = sort_args(c.args)
289289

290290
arguments = ", " + ", ".join(
291-
[get_argument_type(i) for i in sorted_args]
291+
[get_argument_type(i) for i in sorted_args if i != ("flags", "#")]
292292
) if c.args else ""
293293

294294
fields = "\n ".join(
295-
["self.{0} = {0} # {1}".format(i[0], i[1]) for i in c.args]
295+
["self.{0} = {0} # {1}".format(i[0], i[1]) for i in c.args if i != ("flags", "#")]
296296
) if c.args else "pass"
297297

298298
docstring_args = []
299299
docs = c.docs.split("|")[1:] if c.docs else None
300300

301301
for i, arg in enumerate(sorted_args):
302+
if arg == ("flags", "#"):
303+
continue
304+
302305
arg_name, arg_type = arg
303306
is_optional = FLAGS_RE.match(arg_type)
304307
flag_number = is_optional.group(1) if is_optional else -1
@@ -338,28 +341,31 @@ def start():
338341
if references:
339342
docstring_args += "\n\n See Also:\n This object can be returned by " + references + "."
340343

341-
if c.has_flags:
342-
write_flags = []
343-
for i in c.args:
344-
flag = FLAGS_RE.match(i[1])
345-
if flag:
346-
write_flags.append("flags |= (1 << {}) if self.{} is not None else 0".format(flag.group(1), i[0]))
347-
348-
write_flags = "\n ".join([
349-
"flags = 0",
350-
"\n ".join(write_flags),
351-
"b.write(Int(flags))"
352-
])
353-
else:
354-
write_flags = "# No flags"
355-
356-
read_flags = "flags = Int.read(b)" if c.has_flags else "# No flags"
357-
358-
write_types = read_types = ""
344+
write_types = read_types = "" if c.has_flags else "# No flags\n "
359345

360346
for arg_name, arg_type in c.args:
361347
flag = FLAGS_RE_2.findall(arg_type)
362348

349+
if arg_name == "flags" and arg_type == "#":
350+
write_flags = []
351+
352+
for i in c.args:
353+
flag = FLAGS_RE.match(i[1])
354+
if flag:
355+
write_flags.append(
356+
"flags |= (1 << {}) if self.{} is not None else 0".format(flag.group(1), i[0]))
357+
358+
write_flags = "\n ".join([
359+
"flags = 0",
360+
"\n ".join(write_flags),
361+
"b.write(Int(flags))\n "
362+
])
363+
364+
write_types += write_flags
365+
read_types += "flags = Int.read(b)\n "
366+
367+
continue
368+
363369
if flag:
364370
index, flag_type = flag[0]
365371

@@ -448,11 +454,9 @@ def start():
448454
object_id=c.id,
449455
arguments=arguments,
450456
fields=fields,
451-
read_flags=read_flags,
452457
read_types=read_types,
453-
write_flags=write_flags,
454458
write_types=write_types,
455-
return_arguments=", ".join([i[0] for i in sorted_args])
459+
return_arguments=", ".join([i[0] for i in sorted_args if i != ("flags", "#")])
456460
)
457461
)
458462

compiler/api/template/mtproto.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ class {class_name}(Object):
1616

1717
@staticmethod
1818
def read(b: BytesIO, *args) -> "{class_name}":
19-
{read_flags}
2019
{read_types}
2120
return {class_name}({return_arguments})
2221

2322
def write(self) -> bytes:
2423
b = BytesIO()
2524
b.write(Int(self.ID, False))
2625

27-
{write_flags}
2826
{write_types}
2927
return b.getvalue()

0 commit comments

Comments
 (0)