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

'TypeError: buf must be a byte string' using Python 2 #161

Open
asmeurer opened this issue Nov 6, 2014 · 7 comments
Open

'TypeError: buf must be a byte string' using Python 2 #161

asmeurer opened this issue Nov 6, 2014 · 7 comments

Comments

@asmeurer
Copy link

asmeurer commented Nov 6, 2014

In Python 2, I keep getting

  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/api.py", line 94, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/adapters.py", line 362, in send
    timeout=timeout
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 516, in urlopen
    body=body, headers=headers)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 308, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/httplib.py", line 995, in request
    self._send_request(method, url, body, headers)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/httplib.py", line 1029, in _send_request
    self.endheaders(body)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/httplib.py", line 991, in endheaders
    self._send_output(message_body)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/httplib.py", line 844, in _send_output
    self.send(msg)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/httplib.py", line 820, in send
    self.sock.sendall(data)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 203, in sendall
    return self.connection.sendall(data)
  File "/Users/aaronmeurer/anaconda/envs/sympy-release/lib/python2.7/site-packages/OpenSSL/SSL.py", line 970, in sendall
    raise TypeError("buf must be a byte string")
TypeError: buf must be a byte string

I debugged, and I am pretty sure it is coming from requests-oauthlib, which converts the body into a unicode string somewhere when it does its thing with the OAuth2 token (it could also be oauthlib, I haven't debugged that much). The only way I could work around it was to edit that SSL.py and make it encode the buf instead of raising an exception (I would use Python 3 except I have a hard dependency on fabric which doesn't support it yet).

I looked and requests-oauthlib and oauthlib are both making heavy use of from __future__ import unicode_literals, which if I had to guess, is probably the source of the problem.

@ib-lundgren
Copy link
Member

Python 2 is fine and if its caused by requests oauthlib or oauthlib we will fix it.

Not sure I can reproduce from the trace alone, can you please include instructions for reproducing and/or logging output for both the 'oauthlib' and 'requests-oauthlib' namespace. Cheers!

@asmeurer
Copy link
Author

asmeurer commented Nov 8, 2014

@avidas
Copy link

avidas commented Feb 17, 2015

+1 ran into the same issue on Python 2.6

@asmeurer
Copy link
Author

Removing from __future__ import unicode_literals from the code will likely fix this. Its use is typically a bad idea (unlike the other __future__ imports) because not everything in Python 2 "just works" if passed a unicode string instead of a str (as this error shows).

@cosimo
Copy link

cosimo commented Dec 29, 2015

Had the same problem with Python 2.7 on Ubuntu 14.04 LTS.
I tried removing the from __future__ import unicode_literals statements but that didn't really work for me.

@sigmavirus24
Copy link
Contributor

@cosimo as @ib-lundgren pointed out. It's difficult to reproduce this by traceback alone. Can you provide more context to help repro this?

@cosimo
Copy link

cosimo commented Dec 29, 2015

I can start from the SSL error and trace my steps back, but I have no clear idea of what might be the underlying issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants