Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a8abcc

Browse files
authored
InputFile: Fix proper naming of file when reading from subprocess.PIPE (python-telegram-bot#1079)
1 parent 5ff34fc commit 0a8abcc

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

telegram/files/inputfile.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def __init__(self, data):
7070
self.input_file_content = self.input_file.read()
7171
if 'filename' in data:
7272
self.filename = self.data.pop('filename')
73-
elif hasattr(self.input_file, 'name'):
73+
elif (hasattr(self.input_file, 'name') and
74+
not isinstance(self.input_file.name, int) and # py3
75+
self.input_file.name != '<fdopen>'): # py2
7476
# on py2.7, pylint fails to understand this properly
7577
# pylint: disable=E1101
7678
self.filename = os.path.basename(self.input_file.name)

tests/test_inputfile.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# A library that provides a Python interface to the Telegram Bot API
5+
# Copyright (C) 2015-2018
6+
# Leandro Toledo de Souza <[email protected]>
7+
#
8+
# This program is free software: you can redistribute it and/or modify
9+
# it under the terms of the GNU Lesser Public License as published by
10+
# the Free Software Foundation, either version 3 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# This program is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU Lesser Public License for more details.
17+
#
18+
# You should have received a copy of the GNU Lesser Public License
19+
# along with this program. If not, see [http://www.gnu.org/licenses/].
20+
import os
21+
import subprocess
22+
import sys
23+
24+
from telegram import InputFile
25+
26+
27+
class TestInputFile(object):
28+
png = os.path.join('tests', 'data', 'game.png')
29+
30+
def test_subprocess_pipe(self):
31+
if sys.platform == 'win32':
32+
cmd = ['type', self.png]
33+
else:
34+
cmd = ['cat', self.png]
35+
36+
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=(sys.platform == 'win32'))
37+
in_file = InputFile({'photo': proc.stdout})
38+
39+
assert in_file.input_file_content == open(self.png, 'rb').read()
40+
assert in_file.mimetype == 'image/png'
41+
assert in_file.filename == 'image.png'
42+
43+
proc.kill()

0 commit comments

Comments
 (0)