Repository: python-telegram-bot/python-telegram-bot

Issue:
send_photo file from disk doesn't seem to work on python2
### Steps to reproduce
1.  https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#post-an-image-file-from-disk

2. I'm using the API and I'm getting this error:
```
'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)
2018-04-24 09:49:59,039 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\telegram\ext\dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "C:\Python27\lib\site-packages\telegram\ext\commandhandler.py", line 173, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "bot_status.py", line 101, in graph_progress
    bot.send_photo(chat_id, open(photo, 'rb'))
  File "C:\Python27\lib\site-packages\telegram\bot.py", line 60, in decorator
    result = func(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\telegram\bot.py", line 85, in decorator
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
  File "C:\Python27\lib\site-packages\telegram\utils\request.py", line 270, in post
    'POST', url, body=data.to_form(), headers=data.headers, **urlopen_kwargs)
  File "C:\Python27\lib\site-packages\telegram\files\inputfile.py", line 127, in to_form
    return self._parse(form)
  File "C:\Python27\lib\site-packages\telegram\files\inputfile.py", line 141, in _parse
    return '\r\n'.join(form)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x89 in position 0: ordinal not in range(128)
```

3.

### Expected behaviour
I was supposed to get an image

### Actual behaviour
The bot raised an exception

I've tested the same code on python3 and it works correctly, it seems to be a python2 only issue.
In the _parse function it seems that element form[5] is unicode which forces python to treat everything as unicode and PNG is not a valid utf8 data.

### Configuration
Windows 10 x64 1803


**Version of Python, python-telegram-bot & dependencies:**

``$ python -m telegram``
```
python-telegram-bot 10.0.2
certifi 2018.04.16
future 0.16.0
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]
```



Discussion:

Resolving patch:
diff --git a/telegram/files/inputfile.py b/telegram/files/inputfile.py
--- a/telegram/files/inputfile.py
+++ b/telegram/files/inputfile.py
@@ -86,6 +86,10 @@ def __init__(self, data):
                 else:
                     self.mimetype = DEFAULT_MIME_TYPE
 
+        if sys.version_info < (3,):
+            if isinstance(self.filename, unicode):  # flake8: noqa  pylint: disable=E0602
+                self.filename = self.filename.encode('utf-8', 'replace')
+
     @property
     def headers(self):
         """:obj:`dict`: Headers."""


Test patch:
diff --git a/tests/test_photo.py b/tests/test_photo.py
--- a/tests/test_photo.py
+++ b/tests/test_photo.py
@@ -27,7 +27,7 @@
 
 @pytest.fixture(scope='function')
 def photo_file():
-    f = open('tests/data/telegram.jpg', 'rb')
+    f = open(u'tests/data/telegram.jpg', 'rb')
     yield f
     f.close()