mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-24 16:24:15 +03:00
Fix get_input_* not skipping None entities (#215)
This was an issue when parsing optional parameters, since None could not be specified, resulting in a strange crash.
This commit is contained in:
parent
b4811261e9
commit
8bff10d956
|
@ -4,6 +4,7 @@ to convert between an entity like an User, Chat, etc. into its Input version)
|
||||||
"""
|
"""
|
||||||
from mimetypes import add_type, guess_extension
|
from mimetypes import add_type, guess_extension
|
||||||
|
|
||||||
|
from .tl import TLObject
|
||||||
from .tl.types import (
|
from .tl.types import (
|
||||||
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
||||||
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
|
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
|
||||||
|
@ -56,9 +57,20 @@ def get_extension(media):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
def _raise_cast_fail(entity, target):
|
||||||
|
raise ValueError('Cannot cast {} to any kind of {}.'
|
||||||
|
.format(type(entity).__name__, target))
|
||||||
|
|
||||||
|
|
||||||
def get_input_peer(entity):
|
def get_input_peer(entity):
|
||||||
"""Gets the input peer for the given "entity" (user, chat or channel).
|
"""Gets the input peer for the given "entity" (user, chat or channel).
|
||||||
A ValueError is raised if the given entity isn't a supported type."""
|
A ValueError is raised if the given entity isn't a supported type."""
|
||||||
|
if entity is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not isinstance(entity, TLObject):
|
||||||
|
_raise_cast_fail(entity, 'InputPeer')
|
||||||
|
|
||||||
if type(entity).subclass_of_id == 0xc91c90b6: # crc32(b'InputPeer')
|
if type(entity).subclass_of_id == 0xc91c90b6: # crc32(b'InputPeer')
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
@ -92,12 +104,17 @@ def get_input_peer(entity):
|
||||||
if isinstance(entity, PeerChat):
|
if isinstance(entity, PeerChat):
|
||||||
return InputPeerChat(entity.chat_id)
|
return InputPeerChat(entity.chat_id)
|
||||||
|
|
||||||
raise ValueError('Cannot cast {} to any kind of InputPeer.'
|
_raise_cast_fail(entity, 'InputPeer')
|
||||||
.format(type(entity).__name__))
|
|
||||||
|
|
||||||
|
|
||||||
def get_input_channel(entity):
|
def get_input_channel(entity):
|
||||||
"""Similar to get_input_peer, but for InputChannel's alone"""
|
"""Similar to get_input_peer, but for InputChannel's alone"""
|
||||||
|
if entity is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not isinstance(entity, TLObject):
|
||||||
|
_raise_cast_fail(entity, 'InputChannel')
|
||||||
|
|
||||||
if type(entity).subclass_of_id == 0x40f202fd: # crc32(b'InputChannel')
|
if type(entity).subclass_of_id == 0x40f202fd: # crc32(b'InputChannel')
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
@ -107,12 +124,17 @@ def get_input_channel(entity):
|
||||||
if isinstance(entity, InputPeerChannel):
|
if isinstance(entity, InputPeerChannel):
|
||||||
return InputChannel(entity.channel_id, entity.access_hash)
|
return InputChannel(entity.channel_id, entity.access_hash)
|
||||||
|
|
||||||
raise ValueError('Cannot cast {} to any kind of InputChannel.'
|
_raise_cast_fail(entity, 'InputChannel')
|
||||||
.format(type(entity).__name__))
|
|
||||||
|
|
||||||
|
|
||||||
def get_input_user(entity):
|
def get_input_user(entity):
|
||||||
"""Similar to get_input_peer, but for InputUser's alone"""
|
"""Similar to get_input_peer, but for InputUser's alone"""
|
||||||
|
if entity is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not isinstance(entity, TLObject):
|
||||||
|
_raise_cast_fail(entity, 'InputUser')
|
||||||
|
|
||||||
if type(entity).subclass_of_id == 0xe669bf46: # crc32(b'InputUser')
|
if type(entity).subclass_of_id == 0xe669bf46: # crc32(b'InputUser')
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
@ -131,8 +153,7 @@ def get_input_user(entity):
|
||||||
if isinstance(entity, InputPeerUser):
|
if isinstance(entity, InputPeerUser):
|
||||||
return InputUser(entity.user_id, entity.access_hash)
|
return InputUser(entity.user_id, entity.access_hash)
|
||||||
|
|
||||||
raise ValueError('Cannot cast {} to any kind of InputUser.'
|
_raise_cast_fail(entity, 'InputUser')
|
||||||
.format(type(entity).__name__))
|
|
||||||
|
|
||||||
|
|
||||||
def find_user_or_chat(peer, users, chats):
|
def find_user_or_chat(peer, users, chats):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user