Support bytestring URLs on Python 3.x
authorJoe Alcorn <joealcorn123@gmail.com>
Mon, 29 Sep 2014 20:11:45 +0000 (21:11 +0100)
committerJoe Alcorn <joealcorn123@gmail.com>
Tue, 30 Sep 2014 18:29:10 +0000 (19:29 +0100)
AUTHORS.rst
requests/models.py
test_requests.py

index 1e084d8f6b129ba563a2169e273f9a76e94bfb20..7c02b4c050edb0473454b455c12bda15cc88e52d 100644 (file)
@@ -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>`_)
index bbf08c81358cfdd208c1301cbc209855355da178..c5cb3f56ba84ebb6fdebadbc2f2698e4e7199408 100644 (file)
@@ -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
index 4fccc3468addb7e7c1eb2cd31b28f2029aafe4dd..ae879cd1e7758de58bb0a47859ef4e2fd206a446 100755 (executable)
@@ -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 = {