Fix reading vectors of bare longs

This commit is contained in:
Dan 2021-09-10 20:33:09 +02:00
parent eec7ec3947
commit e3f1642797

View File

@ -19,7 +19,7 @@
from io import BytesIO from io import BytesIO
from typing import cast, Union, Any from typing import cast, Union, Any
from .int import Int from .int import Int, Long
from ..list import List from ..list import List
from ..tl_object import TLObject from ..tl_object import TLObject
@ -30,19 +30,29 @@ class Vector(bytes, TLObject):
# Method added to handle the special case when a query returns a bare Vector (of Ints); # Method added to handle the special case when a query returns a bare Vector (of Ints);
# i.e., RpcResult body starts with 0x1cb5c415 (Vector Id) - e.g., messages.GetMessagesViews. # i.e., RpcResult body starts with 0x1cb5c415 (Vector Id) - e.g., messages.GetMessagesViews.
@staticmethod @staticmethod
def _read(b: BytesIO) -> Union[int, Any]: def read_bare(b: BytesIO, n: int) -> Union[int, Any]:
left = len(b.read())
size = left // n
b.seek(-left, 1)
try: try:
return TLObject.read(b) return TLObject.read(b)
except ValueError: except KeyError:
b.seek(-4, 1) b.seek(-4, 1)
return Int.read(b)
if size == 4:
return Int.read(b)
else:
return Long.read(b)
@classmethod @classmethod
def read(cls, data: BytesIO, t: Any = None, *args: Any) -> List: def read(cls, data: BytesIO, t: Any = None, *args: Any) -> List:
count = Int.read(data)
return List( return List(
t.read(data) if t t.read(data) if t
else Vector._read(data) else Vector.read_bare(data, count)
for _ in range(Int.read(data)) for _ in range(count)
) )
def __new__(cls, value: list, t: Any = None) -> bytes: # type: ignore def __new__(cls, value: list, t: Any = None) -> bytes: # type: ignore