Skip to content

Commit 6bd9ddc

Browse files
committed
Add __slots__ to Telegram TL types
1 parent c3470b2 commit 6bd9ddc

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

compiler/api/compiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ def start():
456456
fields=fields,
457457
read_types=read_types,
458458
write_types=write_types,
459-
return_arguments=", ".join([i[0] for i in sorted_args if i != ("flags", "#")])
459+
return_arguments=", ".join([i[0] for i in sorted_args if i != ("flags", "#")]),
460+
slots=", ".join(['"{}"'.format(i[0]) for i in sorted_args if i != ("flags", "#")]),
461+
qualname="{}{}".format("{}.".format(c.namespace) if c.namespace else "", c.name)
460462
)
461463
)
462464

compiler/api/template/mtproto.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class {class_name}(Object):
99
"""{docstring_args}
1010
"""
1111

12+
__slots__ = [{slots}]
13+
1214
ID = {object_id}
15+
QUALNAME = "{qualname}"
1316

1417
def __init__(self{arguments}):
1518
{fields}

pyrogram/api/core/object.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919
from collections import OrderedDict
2020
from datetime import datetime
2121
from io import BytesIO
22-
from json import JSONEncoder, dumps
23-
24-
from ..all import objects
22+
from json import dumps
2523

2624

2725
class Object:
2826
all = {}
2927

28+
__slots__ = []
29+
30+
QUALNAME = "Base"
31+
3032
@staticmethod
3133
def read(b: BytesIO, *args):
3234
return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args)
@@ -35,7 +37,7 @@ def write(self, *args) -> bytes:
3537
pass
3638

3739
def __str__(self) -> str:
38-
return dumps(self, cls=Encoder, indent=4)
40+
return dumps(self, indent=4, default=default)
3941

4042
def __bool__(self) -> bool:
4143
return True
@@ -62,29 +64,18 @@ def remove_none(obj):
6264
return obj
6365

6466

65-
class Encoder(JSONEncoder):
66-
def default(self, o: Object):
67-
try:
68-
content = o.__dict__
69-
except AttributeError:
70-
if isinstance(o, datetime):
71-
return o.strftime("%d-%b-%Y %H:%M:%S")
72-
else:
73-
return repr(o)
74-
75-
name = o.__class__.__name__
76-
o = objects.get(getattr(o, "ID", None), None)
77-
78-
if o is not None:
79-
if o.startswith("pyrogram.client"):
80-
r = remove_none(OrderedDict([("_", "pyrogram:" + name)] + [i for i in content.items()]))
81-
r.pop("_client", None)
82-
83-
return r
84-
else:
85-
return OrderedDict(
86-
[("_", o.replace("pyrogram.api.types.", "telegram:"))]
87-
+ [i for i in content.items()]
88-
)
67+
def default(o: "Object"):
68+
try:
69+
content = {i: getattr(o, i) for i in o.__slots__}
70+
71+
return remove_none(
72+
OrderedDict(
73+
[("_", o.QUALNAME)]
74+
+ [i for i in content.items()]
75+
)
76+
)
77+
except AttributeError:
78+
if isinstance(o, datetime):
79+
return o.strftime("%d-%b-%Y %H:%M:%S")
8980
else:
90-
return None
81+
return repr(o)

0 commit comments

Comments
 (0)