diff --git a/client/src/telethon/_impl/tl/core/reader.py b/client/src/telethon/_impl/tl/core/reader.py index 45d8bad7..703b3d1b 100644 --- a/client/src/telethon/_impl/tl/core/reader.py +++ b/client/src/telethon/_impl/tl/core/reader.py @@ -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("> 8 diff --git a/client/src/telethon/_impl/tl/core/serializable.py b/client/src/telethon/_impl/tl/core/serializable.py index 7161c50d..acadc89b 100644 --- a/client/src/telethon/_impl/tl/core/serializable.py +++ b/client/src/telethon/_impl/tl/core/serializable.py @@ -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})" diff --git a/generator/src/telethon_generator/_impl/codegen/serde/deserialization.py b/generator/src/telethon_generator/_impl/codegen/serde/deserialization.py index 3c0c7b1d..1faf69d8 100644 --- a/generator/src/telethon_generator/_impl/codegen/serde/deserialization.py +++ b/generator/src/telethon_generator/_impl/codegen/serde/deserialization.py @@ -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":