Skip to content

Failure when uploading binary files on Python 3 when resumable=False #145

@Rufflewind

Description

@Rufflewind

Seems like MediaFileUpload (or something else deep inside) is failing to handling binary files properly on Python 3. My guess is that the contents of the file is mistakenly passed as a Unicode string (rather than as a byte-string) into http.client, which dies as soon as it encounters a non-Latin-1 character. This only happens when resumable=False.

Code to reproduce the problem:

import apiclient
service = apiclient.discovery.build("drive", "v2", …)
filename = "some-file.tar.xz"
media = apiclient.http.MediaFileUpload(filename)
service.files().insert(body={"title": os.path.basename(filename)}, media_body=media).execute()

Typical error:

Traceback (most recent call last):
  …
  File "/usr/lib/python3.5/site-packages/oauth2client/util.py", line 142, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/googleapiclient/http.py", line 722, in execute
    body=self.body, headers=self.headers)
  File "/usr/lib/python3.5/site-packages/oauth2client/client.py", line 589, in new_request
    redirections, connection_type)
  File "/usr/lib/python3.5/site-packages/httplib2/__init__.py", line 1313, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/lib/python3.5/site-packages/httplib2/__init__.py", line 1063, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/lib/python3.5/site-packages/httplib2/__init__.py", line 987, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1083, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1127, in _send_request
    body = body.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\ufffd' in position 350: ordinal not in range(256)

(Using Python 3.5.0 on Arch Linux 4.2.2.)

Metadata

Metadata

Labels

🚨This issue needs some love.triage meI really want to be triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions