mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 21:10:29 +00:00
Replace .to_bytes() with the special .__bytes__ function
This commit is contained in:
@@ -13,21 +13,21 @@ class GzipPacked(TLObject):
|
||||
|
||||
@staticmethod
|
||||
def gzip_if_smaller(request):
|
||||
"""Calls request.to_bytes(), and based on a certain threshold,
|
||||
"""Calls bytes(request), and based on a certain threshold,
|
||||
optionally gzips the resulting data. If the gzipped data is
|
||||
smaller than the original byte array, this is returned instead.
|
||||
|
||||
Note that this only applies to content related requests.
|
||||
"""
|
||||
data = request.to_bytes()
|
||||
data = bytes(request)
|
||||
# TODO This threshold could be configurable
|
||||
if request.content_related and len(data) > 512:
|
||||
gzipped = GzipPacked(data).to_bytes()
|
||||
gzipped = bytes(GzipPacked(data))
|
||||
return gzipped if len(gzipped) < len(data) else data
|
||||
else:
|
||||
return data
|
||||
|
||||
def to_bytes(self):
|
||||
def __bytes__(self):
|
||||
# TODO Maybe compress level could be an option
|
||||
return struct.pack('<I', GzipPacked.CONSTRUCTOR_ID) + \
|
||||
TLObject.serialize_bytes(gzip.compress(self.data))
|
||||
|
Reference in New Issue
Block a user