Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python-http.client codegens fails to run if non-text files are being uploaded in the request. #366

Closed
webholik opened this issue Oct 14, 2020 · 0 comments · Fixed by #382
Assignees
Labels
bug Something isn't working

Comments

@webholik
Copy link
Collaborator

webholik commented Oct 14, 2020

The generated code tries to open the file as text without checking if its binary:

with open('file.jpeg') as f:
  dataList.append(f.read())

which leads to the following error:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    dataList.append(f.read())
  File "/usr/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Even if you manually change code to open the file as binary:

with open('file.jpeg', 'rb') as f:
  dataList.append(f.read())

The code still fails:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    body = '\r\n'.join(dataList)
TypeError: sequence item 4: expected str instance, bytes found

This is because the join method fails if the given list contains binary data.

Note: The bug only exists for Python 3 as http.client is Python 3 only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant