From e624ae8aeadc63b473e1d57f1bec71b4f713cf32 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Wed, 8 Aug 2012 11:59:00 +0100 Subject: [PATCH 1/2] Add test for Issue #423. --- tests/test_requests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index dd08b2af..f7803ba1 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -996,5 +996,23 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): first_line = next(req.response.iter_lines()) self.assertTrue(first_line.strip().decode('utf-8').startswith('{')) + def test_accept_objects_with_string_representations_as_urls(self): + """Test that URLs can be set to objects with string representations, + e.g. for use with furl.""" + class URL(): + def __unicode__(self): + # Can't have unicode literals in Python3, so avoid them. + # TODO: fixup when moving to Python 3.3 + if (sys.version_info[0] == 2): + return 'http://httpbin.org/get'.decode('utf-8') + else: + return 'http://httpbin.org/get' + + def __str__(self): + return 'http://httpbin.org/get' + + r = get(URL()) + self.assertEqual(r.status_code, 200) + if __name__ == '__main__': unittest.main() From 6166ba7e131ef77f9748488a02dc5e699928a17d Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Wed, 8 Aug 2012 12:05:52 +0100 Subject: [PATCH 2/2] Accept objects with string representations as URLs. --- requests/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index d8c0f3e8..20a28807 100644 --- a/requests/models.py +++ b/requests/models.py @@ -71,7 +71,14 @@ class Request(object): self.timeout = timeout #: Request URL. - self.url = url + #: Accept objects that have string representations. + try: + self.url = unicode(url) + except NameError: + # We're on Python 3. + self.url = str(url) + except UnicodeDecodeError: + self.url = url #: Dictionary of HTTP Headers to attach to the :class:`Request `. self.headers = dict(headers or [])