From 18660f13dc0295fe79594baf0295aeb5b9b5fb6b Mon Sep 17 00:00:00 2001 From: Paul Fisher Date: Mon, 12 Feb 2018 16:43:58 -0500 Subject: [PATCH 1/2] bpo-32779: Fix urljoining with an empty query string. Previously, urllib.urljoin with a relative URL of the form '?' would result in no change to the URL, in spite of the fact that it should clear the query string. This solves that case and variations on it. --- Lib/test/test_urlparse.py | 6 ++++++ Lib/urllib/parse.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index dff9a8ede9b601..7a3c4ae10cd576 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -437,6 +437,12 @@ def test_urljoins(self): # issue 23703: don't duplicate filename self.checkJoin('a', 'b', 'b') + # issue 32779: clear the query string when joining with '?' + self.checkJoin('http://a/b/c?d=e', '?', 'http://a/b/c') + self.checkJoin('http://a/b/c?d=e', 'http:?', 'http://a/b/c') + self.checkJoin('http://a/b/c#d?e=f', '?#g', 'http://a/b/c#g') + self.checkJoin('http://a/b/c?d=e#f', '#?', 'http://a/b/c?d=e#?') + def test_RFC2732(self): str_cases = [ ('http://Test.python.org:5432/foo/', 'test.python.org', 5432), diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index bf16d0f42e5794..89624201592a1f 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -536,7 +536,10 @@ def urljoin(base, url, allow_fragments=True): if not path and not params: path = bpath params = bparams - if not query: + # since urlparse doesn't leave any evidence of whether there was a bare + # '?' with an empty query string, we need to check whether it was there. + has_empty_query = url[0] == '?' or url.startswith(scheme + ':?') + if not has_empty_query: query = bquery return _coerce_result(urlunparse((scheme, netloc, path, params, query, fragment))) From c6d2bfa62ef02387f06e53efcfb580a20a93efcd Mon Sep 17 00:00:00 2001 From: Paul Fisher Date: Mon, 12 Feb 2018 17:13:52 -0500 Subject: [PATCH 2/2] Add NEWS entry. --- .../NEWS.d/next/Library/2018-02-12-17-12-45.bpo-32779.FYzcZd.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2018-02-12-17-12-45.bpo-32779.FYzcZd.rst diff --git a/Misc/NEWS.d/next/Library/2018-02-12-17-12-45.bpo-32779.FYzcZd.rst b/Misc/NEWS.d/next/Library/2018-02-12-17-12-45.bpo-32779.FYzcZd.rst new file mode 100644 index 00000000000000..6729499008d9f0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-02-12-17-12-45.bpo-32779.FYzcZd.rst @@ -0,0 +1 @@ +Fix urljoining with '?', a URL containing only a bare query string.