summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Alcorn <joealcorn123@gmail.com>2014-09-29 21:11:45 +0100
committerJoe Alcorn <joealcorn123@gmail.com>2014-09-30 19:29:10 +0100
commita68d1b4517395e4845fa8405f043f8aa3110f83c (patch)
tree100e3d329ddf420b1b823a410867b16d9bf3719f
parenta718a81d273503bd2ffae8e6cb036a8516eb426a (diff)
downloadpython-requests-a68d1b4517395e4845fa8405f043f8aa3110f83c.tar.gz
Support bytestring URLs on Python 3.x
-rw-r--r--AUTHORS.rst1
-rw-r--r--requests/models.py13
-rwxr-xr-xtest_requests.py6
3 files changed, 14 insertions, 6 deletions
diff --git a/AUTHORS.rst b/AUTHORS.rst
index 1e084d8f..7c02b4c0 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -155,3 +155,4 @@ Patches and Suggestions
- Ben Bass (`@codedstructure <https://github.com/codedstructure>`_)
- Jonathan Wong <evolutionace@gmail.com> (`@ContinuousFunction <https://github.com/ContinuousFunction>`_)
- Martin Jul (`@mjul <https://github.com/mjul>`_)
+- Joe Alcorn (`@buttscicles <https://github.com/buttscicles>`_)
diff --git a/requests/models.py b/requests/models.py
index bbf08c81..c5cb3f56 100644
--- a/requests/models.py
+++ b/requests/models.py
@@ -326,13 +326,14 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
def prepare_url(self, url, params):
"""Prepares the given HTTP URL."""
#: Accept objects that have string representations.
+ #: We're unable to blindy call unicode/str functions
+ #: as this will include the bytestring indicator (b'')
+ #: on python 3.x.
+ #: https://github.com/kennethreitz/requests/pull/2238
try:
- url = unicode(url)
- except NameError:
- # We're on Python 3.
- url = str(url)
- except UnicodeDecodeError:
- pass
+ url = url.decode('utf8')
+ except AttributeError:
+ url = unicode(url) if is_py2 else str(url)
# Don't do any URL preparation for non-HTTP schemes like `mailto`,
# `data` etc to work around exceptions from `url_parse`, which
diff --git a/test_requests.py b/test_requests.py
index 4fccc346..ae879cd1 100755
--- a/test_requests.py
+++ b/test_requests.py
@@ -591,6 +591,12 @@ class RequestsTestCase(unittest.TestCase):
assert resp.json()['headers'][
'Dummy-Auth-Test'] == 'dummy-auth-test-ok'
+ def test_prepare_request_with_bytestring_url(self):
+ req = requests.Request('GET', b'https://httpbin.org/')
+ s = requests.Session()
+ prep = s.prepare_request(req)
+ assert prep.url == "https://httpbin.org/"
+
def test_links(self):
r = requests.Response()
r.headers = {