mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
Avoid storing memoryview in deserialized objects
This commit is contained in:
parent
352ccdd4fc
commit
9e582ca0fa
|
@ -31,17 +31,17 @@ def _bootstrap_get_ty(constructor_id: int) -> Optional[Type["Serializable"]]:
|
|||
class Reader:
|
||||
__slots__ = ("_view", "_pos", "_len")
|
||||
|
||||
def __init__(self, buffer: bytes) -> None:
|
||||
def __init__(self, buffer: bytes | memoryview) -> None:
|
||||
self._view = (
|
||||
memoryview(buffer) if not isinstance(buffer, memoryview) else buffer
|
||||
)
|
||||
self._pos = 0
|
||||
self._len = len(self._view)
|
||||
|
||||
def read_remaining(self) -> bytes:
|
||||
def read_remaining(self) -> memoryview:
|
||||
return self.read(self._len - self._pos)
|
||||
|
||||
def read(self, n: int) -> bytes:
|
||||
def read(self, n: int) -> memoryview:
|
||||
self._pos += n
|
||||
assert self._pos <= self._len
|
||||
return self._view[self._pos - n : self._pos]
|
||||
|
@ -52,7 +52,7 @@ class Reader:
|
|||
assert self._pos <= self._len
|
||||
return struct.unpack(fmt, self._view[self._pos - size : self._pos])
|
||||
|
||||
def read_bytes(self) -> bytes:
|
||||
def read_bytes(self) -> memoryview:
|
||||
if self._view[self._pos] == 254:
|
||||
self._pos += 4
|
||||
length = struct.unpack("<i", self._view[self._pos - 4 : self._pos])[0] >> 8
|
||||
|
|
|
@ -36,10 +36,6 @@ class Serializable(abc.ABC):
|
|||
|
||||
def __repr__(self) -> str:
|
||||
fields = ((attr, getattr(self, attr)) for attr in self.__slots__)
|
||||
fields = (
|
||||
(name, bytes(field) if isinstance(field, memoryview) else field)
|
||||
for name, field in fields
|
||||
)
|
||||
attrs = ", ".join(f"{name}={field!r}" for name, field in fields)
|
||||
return f"{self.__class__.__name__}({attrs})"
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ from .common import inner_type_fmt, is_trivial, to_class_name, trivial_struct_fm
|
|||
# Some implementations choose to create these types by hand.
|
||||
# For consistency, we instead special-case the generator.
|
||||
SPECIAL_CASED_OBJECT_READS = {
|
||||
0xF35C6D01: "reader.read_remaining()", # rpc_result
|
||||
0x5BB8E511: "reader.read(_bytes)", # message
|
||||
0xF35C6D01: "b'' + reader.read_remaining()", # rpc_result
|
||||
0x5BB8E511: "b'' + reader.read(_bytes)", # message
|
||||
}
|
||||
|
||||
|
||||
|
@ -22,7 +22,7 @@ def reader_read_fmt(ty: Type, constructor_id: int) -> Tuple[str, Optional[str]]:
|
|||
elif ty.name == "string":
|
||||
return f"str(reader.read_bytes(), 'utf-8', 'replace')", None
|
||||
elif ty.name == "bytes":
|
||||
return f"reader.read_bytes()", None
|
||||
return f"b'' + reader.read_bytes()", None
|
||||
elif ty.name == "int128":
|
||||
return f"int.from_bytes(reader.read(16))", None
|
||||
elif ty.name == "int256":
|
||||
|
|
Loading…
Reference in New Issue
Block a user