Skip to content

Commit 8954f86

Browse files
committed
pythongh-99901: prepend '/' to url only if netloc is not empty.
1 parent cc13eab commit 8954f86

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/test/test_urlparse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,13 @@ def test_default_scheme(self):
875875
self.assertEqual(func(b"path").scheme, b"")
876876
self.assertEqual(func(b"path", "").scheme, b"")
877877

878+
def test_empty_netloc(self):
879+
# Issue gh-99901: don't preprend '/' to url if netloc is empty
880+
self.assertEqual(urllib.parse.urlunparse(("http", "", "example", "", "", "")),
881+
"http://example")
882+
self.assertEqual(urllib.parse.urlunparse((b"http", b"", b"example", b"", b"", b"")),
883+
b"http://example")
884+
878885
def test_parse_fragments(self):
879886
# Exercise the allow_fragments parameter of urlparse() and urlsplit()
880887
tests = (

Lib/urllib/parse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ def urlunsplit(components):
526526
scheme, netloc, url, query, fragment, _coerce_result = (
527527
_coerce_args(*components))
528528
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
529-
if url and url[:1] != '/': url = '/' + url
529+
if netloc and url and url[:1] != '/':
530+
url = '/' + url
530531
url = '//' + (netloc or '') + url
531532
if scheme:
532533
url = scheme + ':' + url
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`urllib.parse.urlunsplit` to prepend '/' to url only if netloc is not empty.

0 commit comments

Comments
 (0)