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

@@ -5,6 +5,7 @@ to convert between an entity like an User, Chat, etc. into its Input version)
import math
import mimetypes
import re
import types
from mimetypes import add_type, guess_extension
from .tl.types import (
@@ -341,6 +342,17 @@ def is_video(file):
(mimetypes.guess_type(file)[0] or '').startswith('video/'))
def is_list_like(obj):
"""
Returns True if the given object looks like a list.
Checking if hasattr(obj, '__iter__') and ignoring str/bytes is not
enough. Things like open() are also iterable (and probably many
other things), so just support the commonly known list-like objects.
"""
return isinstance(obj, (list, tuple, set, dict, types.GeneratorType))
def parse_phone(phone):
"""Parses the given phone, or returns None if it's invalid"""
if isinstance(phone, int):