From 01a95421d95e1dbf1af29858736d930198754b14 Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Sun, 31 Dec 2017 14:30:34 -0800 Subject: [PATCH 1/3] add and test URL.__str__, which enables URLs to be passed directly to libaries which stringify (like requests). fixes #49. --- hyperlink/_url.py | 15 +++++++++++++++ hyperlink/test/test_url.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/hyperlink/_url.py b/hyperlink/_url.py index 3bbb76f8..84c35b06 100644 --- a/hyperlink/_url.py +++ b/hyperlink/_url.py @@ -1343,6 +1343,21 @@ def __repr__(self): """ return '%s.from_text(%r)' % (self.__class__.__name__, self.to_text()) + def _to_bytes(self): + """ + Allows for direct usage of URL objects with libraries like + requests, which automatically stringify URL parameters. See + issue #49. Note that some libraries may not handle IRIs well. + """ + return self.to_text().encode('utf8') + + if PY2: + __str__ = _to_bytes + __unicode__ = to_text + else: + __bytes__ = _to_bytes + __str__ = to_text + # # Begin Twisted Compat Code asURI = to_uri asIRI = to_iri diff --git a/hyperlink/test/test_url.py b/hyperlink/test/test_url.py index bde9a917..ec3737f7 100644 --- a/hyperlink/test/test_url.py +++ b/hyperlink/test/test_url.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals +import sys import socket from .common import HyperlinkTestCase @@ -13,6 +14,8 @@ from .. import _url from .._url import inet_pton, SCHEME_PORT_MAP, parse_host + +PY2 = (sys.version_info[0] == 2) unicode = type(u'') @@ -1150,3 +1153,17 @@ def test_normalize(self): # test invalid percent encoding during normalize assert URL(path=('', '%te%sts')).normalize().to_text() == '/%te%sts' + + def test_str(self): + # see also issue #49 + text = u'http://example.com/á/y%20a%20y/?b=%25' + url = URL.from_text(text) + assert unicode(url) == text + assert bytes(url) == text.encode('utf8') + + if PY2: + assert isinstance(str(url), bytes) + assert isinstance(unicode(url), unicode) + else: + assert isinstance(str(url), unicode) + assert isinstance(bytes(url), bytes) From 0bf0429a578e06a8dc32ce9d42c833ec84d9e48e Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Sun, 31 Dec 2017 15:45:36 -0800 Subject: [PATCH 2/3] switch URL.__str__ to use the to_url approach discussed. --- hyperlink/_url.py | 2 +- hyperlink/test/test_url.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hyperlink/_url.py b/hyperlink/_url.py index 84c35b06..e1949853 100644 --- a/hyperlink/_url.py +++ b/hyperlink/_url.py @@ -1349,7 +1349,7 @@ def _to_bytes(self): requests, which automatically stringify URL parameters. See issue #49. Note that some libraries may not handle IRIs well. """ - return self.to_text().encode('utf8') + return self.to_uri().to_text().encode('ascii') if PY2: __str__ = _to_bytes diff --git a/hyperlink/test/test_url.py b/hyperlink/test/test_url.py index ec3737f7..a1c6d2f1 100644 --- a/hyperlink/test/test_url.py +++ b/hyperlink/test/test_url.py @@ -1159,7 +1159,7 @@ def test_str(self): text = u'http://example.com/á/y%20a%20y/?b=%25' url = URL.from_text(text) assert unicode(url) == text - assert bytes(url) == text.encode('utf8') + assert bytes(url) == b'http://example.com/%C3%A1/y%20a%20y/?b=%25' if PY2: assert isinstance(str(url), bytes) From 04694a0e64e0583e4cb4499e1c9a6a937d62b227 Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Sun, 31 Dec 2017 18:07:06 -0800 Subject: [PATCH 3/3] remove out of date docstring comment --- hyperlink/_url.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hyperlink/_url.py b/hyperlink/_url.py index e1949853..87f1a665 100644 --- a/hyperlink/_url.py +++ b/hyperlink/_url.py @@ -1347,7 +1347,7 @@ def _to_bytes(self): """ Allows for direct usage of URL objects with libraries like requests, which automatically stringify URL parameters. See - issue #49. Note that some libraries may not handle IRIs well. + issue #49. """ return self.to_uri().to_text().encode('ascii')