Skip to content

Encode error when downloading files with non-ASCII filenames. #650

@blueset

Description

@blueset

Steps to reproduce

  1. head /dev/random > 凵冂工匚口わ巨 and send the file to a bot.
import telegram
b = telegram.Bot(TOKEN)
file_id = b.getUpdates()[0].message.document.file_id
b.getFile(file_id).download("./storage")

Expected behaviour

Tell us what should happen

Download the file to specified directory.

Actual behaviour

Tell us what happens instead

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/telegram/file.py", line 106, in download
    self.bot.request.download(url, filename, timeout=timeout)
  File "/usr/local/lib/python3.6/site-packages/telegram/utils/request.py", line 284, in download
    buf = self.retrieve(url, timeout=timeout)
  File "/usr/local/lib/python3.6/site-packages/telegram/utils/request.py", line 270, in retrieve
    return self._request_wrapper('GET', url, **urlopen_kwargs)
  File "/usr/local/lib/python3.6/site-packages/telegram/utils/request.py", line 174, in _request_wrapper
    resp = self._con_pool.request(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/telegram/vendor/ptb_urllib3/urllib3/request.py", line 66, in request
    **urlopen_kw)
  File "/usr/local/lib/python3.6/site-packages/telegram/vendor/ptb_urllib3/urllib3/request.py", line 87, in request_encode_url
    return self.urlopen(method, url, **extra_kw)
  File "/usr/local/lib/python3.6/site-packages/telegram/vendor/ptb_urllib3/urllib3/poolmanager.py", line 244, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/local/lib/python3.6/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 617, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 390, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1250, in _send_request
    self.putrequest(method, url, **skips)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1117, in putrequest
    self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 69-75: ordinal not in range(128)
'ascii' codec can't encode characters in position 69-75: ordinal not in range(128)

Configuration

Operating System:
Tested on:

  • Mac OS X 10.11
  • Ubuntu 16.04

Version of Python, python-telegram-bot & dependencies:

$ python -m telegram

python-telegram-bot 6.0.3
urllib3 1.21.1
certifi 2017.04.17
future 0.16.0
Python 3.6.1 (default, Mar 23 2017, 16:49:01)  [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]

Possible Solution

URL escape the "download path" given by getFile, then download.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions