Skip to content

Commit 252abb1

Browse files
committed
Using hasattr instead isinstance for file check python-telegram-bot#119
1 parent 203364d commit 252abb1

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

telegram/inputfile.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
try:
2424
from email.generator import _make_boundary as choose_boundary
2525
from urllib.request import urlopen
26-
from io import BufferedReader as file
2726
except ImportError:
2827
from mimetools import choose_boundary
2928
from urllib2 import urlopen
@@ -76,13 +75,12 @@ def __init__(self,
7675
else:
7776
from_url = False
7877

79-
if isinstance(self.input_file, file) or from_url:
78+
if hasattr(self.input_file, 'read') or from_url:
8079
self.filename = None
8180
self.input_file_content = self.input_file.read()
8281
if 'filename' in data:
8382
self.filename = self.data.pop('filename')
84-
elif isinstance(self.input_file, file) and \
85-
hasattr(self.input_file, 'name'):
83+
elif hasattr(self.input_file, 'name'):
8684
self.filename = os.path.basename(self.input_file.name)
8785
elif from_url:
8886
self.filename = os.path.basename(self.input_file.url) \
@@ -198,7 +196,7 @@ def is_inputfile(data):
198196
if file_type:
199197
file_content = data[file_type[0]]
200198

201-
return isinstance(file_content, file) or str(
199+
return hasattr(file_content, 'read') or str(
202200
file_content).startswith('http')
203201

204202
return False

tests/test_bot.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ def testSendGIFURLPhoto(self):
152152
self.assertTrue(self.is_json(message.to_json()))
153153
self.assertEqual(message.photo[0].file_size, 684)
154154

155+
@flaky(3, 1)
156+
@timeout(10)
157+
def testSendBufferedReaderPhoto(self):
158+
photo = open('tests/data/telegram.png', 'rb')
159+
message = self._bot.sendPhoto(photo=photo,
160+
chat_id=self._chat_id)
161+
162+
self.assertTrue(self.is_json(message.to_json()))
163+
self.assertEqual(message.photo[0].file_size, 1451)
164+
155165
@flaky(3, 1)
156166
@timeout(10)
157167
def testSendChatAction(self):

0 commit comments

Comments
 (0)