From a68d1b4517395e4845fa8405f043f8aa3110f83c Mon Sep 17 00:00:00 2001 From: Joe Alcorn Date: Mon, 29 Sep 2014 21:11:45 +0100 Subject: Support bytestring URLs on Python 3.x --- AUTHORS.rst | 1 + requests/models.py | 13 +++++++------ test_requests.py | 6 ++++++ 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 `_) - Jonathan Wong (`@ContinuousFunction `_) - Martin Jul (`@mjul `_) +- Joe Alcorn (`@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 = { -- cgit v1.2.1