From ca2ebc5b682452bc870efb2f7123feda9a80a8ea Mon Sep 17 00:00:00 2001 From: MinRK Date: Sun, 5 Oct 2014 16:30:11 -0700 Subject: [PATCH] allow unicode URLs on Python 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 | 4 ++-- test_requests.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index 245521dc..aaa50632 100644 --- a/requests/models.py +++ b/requests/models.py @@ -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`, diff --git a/test_requests.py b/test_requests.py index 8864b2d1..0d93893b 100755 --- a/test_requests.py +++ b/test_requests.py @@ -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()