Skip to content

Commit e74791d

Browse files
committed
Merge pull request #2238 from buttscicles/byte-urls
Support bytestring URLs on Python 3.x
2 parents a718a81 + a68d1b4 commit e74791d

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,4 @@ Patches and Suggestions
155155
- Ben Bass (`@codedstructure <https://github.com/codedstructure>`_)
156156
- Jonathan Wong <evolutionace@gmail.com> (`@ContinuousFunction <https://github.com/ContinuousFunction>`_)
157157
- Martin Jul (`@mjul <https://github.com/mjul>`_)
158+
- Joe Alcorn (`@buttscicles <https://github.com/buttscicles>`_)

requests/models.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,14 @@ def prepare_method(self, method):
326326
def prepare_url(self, url, params):
327327
"""Prepares the given HTTP URL."""
328328
#: Accept objects that have string representations.
329+
#: We're unable to blindy call unicode/str functions
330+
#: as this will include the bytestring indicator (b'')
331+
#: on python 3.x.
332+
#: https://github.com/kennethreitz/requests/pull/2238
329333
try:
330-
url = unicode(url)
331-
except NameError:
332-
# We're on Python 3.
333-
url = str(url)
334-
except UnicodeDecodeError:
335-
pass
334+
url = url.decode('utf8')
335+
except AttributeError:
336+
url = unicode(url) if is_py2 else str(url)
336337

337338
# Don't do any URL preparation for non-HTTP schemes like `mailto`,
338339
# `data` etc to work around exceptions from `url_parse`, which

test_requests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ def __call__(self, r):
591591
assert resp.json()['headers'][
592592
'Dummy-Auth-Test'] == 'dummy-auth-test-ok'
593593

594+
def test_prepare_request_with_bytestring_url(self):
595+
req = requests.Request('GET', b'https://httpbin.org/')
596+
s = requests.Session()
597+
prep = s.prepare_request(req)
598+
assert prep.url == "https://httpbin.org/"
599+
594600
def test_links(self):
595601
r = requests.Response()
596602
r.headers = {

0 commit comments

Comments
 (0)