allow unicode URLs on Python 2
authorMinRK <benjaminrk@gmail.com>
Sun, 5 Oct 2014 23:30:11 +0000 (16:30 -0700)
committerMinRK <benjaminrk@gmail.com>
Sun, 5 Oct 2014 23:30:11 +0000 (16:30 -0700)
on Python 2 u'é'.decode('utf8') fails with UnicodeEncodeError,
but only AttributeError is caught.

This only calls decode on known bytes objects.

requests/models.py
test_requests.py

index 245521dc27c9e6b2b272cef0f92f2b4b83fe951c..aaa50632ece16e2bd4bf5a1be8059887bbcb1224 100644 (file)
@@ -338,9 +338,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
         #: as this will include the bytestring indicator (b'')
         #: on python 3.x.
         #: https://github.com/kennethreitz/requests/pull/2238
-        try:
+        if isinstance(url, bytes):
             url = url.decode('utf8')
-        except AttributeError:
+        else:
             url = unicode(url) if is_py2 else str(url)
 
         # Don't do any URL preparation for non-HTTP schemes like `mailto`,
index 8864b2d1a3dbc7799757034ec44a1e12d83bcf88..0d93893b9df9f6458431d72edf6b515e728df563 100755 (executable)
@@ -1507,5 +1507,13 @@ def test_prepared_request_complete_copy():
     )
     assert_copy(p, p.copy())
 
+def test_prepare_unicode_url():
+    p = PreparedRequest()
+    p.prepare(
+        method='GET',
+        url=u('http://www.example.com/üniçø∂é')
+    )
+    assert_copy(p, p.copy())
+
 if __name__ == '__main__':
     unittest.main()