Skip to content

TypeError: startswith first arg must be bytes or a tuple of bytes, not str. Python 3. #1366

@michaelhelmick

Description

@michaelhelmick

Python 3

requirements.txt

requests==1.2.0
requests_oauthlib==0.3.1

Stacktrace:

Traceback (most recent call last):
  File "/home/travis/build/ryanmcgrath/twython/test_twython.py", line 293, in test_create_and_destroy_favorite
    self.api.create_favorite(id=test_tweet_id)
  File "/home/travis/build/ryanmcgrath/twython/twython/twython.py", line 87, in <lambda>
    return lambda **kwargs: self._constructFunc(key, deprecated_key, **kwargs)
  File "/home/travis/build/ryanmcgrath/twython/twython/twython.py", line 126, in _constructFunc
    content = self._request(url, method=fn['method'], params=kwargs)
  File "/home/travis/build/ryanmcgrath/twython/twython/twython.py", line 142, in _request
    response = func(url, data=params, files=files)
  File "/home/travis/virtualenv/python3.3/lib/python3.3/site-packages/requests/sessions.py", line 399, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/home/travis/virtualenv/python3.3/lib/python3.3/site-packages/requests/sessions.py", line 354, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/travis/virtualenv/python3.3/lib/python3.3/site-packages/requests/sessions.py", line 455, in send
    adapter = self.get_adapter(url=request.url)
  File "/home/travis/virtualenv/python3.3/lib/python3.3/site-packages/requests/sessions.py", line 489, in get_adapter
    if url.startswith(prefix):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Alright, so basically, my tests are failing.
It ends up being that when using oauthlib (from requests-oauthlib) in conjunction with requests.. oauthlib actually converts the url being passed to bytes and when requests/sessions.py tries to get the adapter on line 455 (requests 1.2.0) in the get_adapter function, it goes through a forloop and at that point (line 489):

 if url.startswith(prefix):

url is bytes from oauthlib.oauth1.Client.sign every point before that it's a str (trust me, I did extensive filtering to see where the hell it was getting converted from str to bytes haha)

Where should this be fixed though?

  • oauthlib.. it should be returning str for the url in the sign function
  • requests_oauthlib.. after it calls self.client.sign from oauthlib.oauth1.Client.sign we should convert r.url back to a str before sending it to requests
    OR
  • requests.. always convert url to a str in get_adapter

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