Avoid relying on .__iter__ to tell iterators apart

.send_file() would fail with stream objects (those from open())
since they are iterable, and asserting that they weren't bytes
or str was not enough.
This commit is contained in:
Lonami Exo
2018-02-26 14:12:21 +01:00
parent 6f16aeb553
commit 5a54e2279f
4 changed files with 19 additions and 7 deletions

View File

@@ -975,7 +975,7 @@ class TelegramClient(TelegramBareClient):
"""
if max_id is None:
if message:
if hasattr(message, '__iter__'):
if utils.is_list_like(message):
max_id = max(msg.id for msg in message)
else:
max_id = message.id
@@ -1140,7 +1140,7 @@ class TelegramClient(TelegramBareClient):
"""
# First check if the user passed an iterable, in which case
# we may want to send as an album if all are photo files.
if hasattr(file, '__iter__') and not isinstance(file, (str, bytes)):
if utils.is_list_like(file):
# Convert to tuple so we can iterate several times
file = tuple(x for x in file)
if all(utils.is_image(x) for x in file):
@@ -1960,7 +1960,7 @@ class TelegramClient(TelegramBareClient):
``User``, ``Chat`` or ``Channel`` corresponding to the input
entity.
"""
if hasattr(entity, '__iter__') and not isinstance(entity, str):
if utils.is_list_like(entity):
single = False
else:
single = True