Skip to content

send_photo file from disk doesn't seem to work on python2 #1083

@yoava333

Description

@yoava333

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)

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)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions