Skip to content

Commit cc9067b

Browse files
Fix urlunparse() and urlunsplit() for URIs with path starting with multiple slashes and no authority.
1 parent eea029d commit cc9067b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Lib/test/test_urlparse.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ def test_qs(self):
142142

143143
def test_roundtrips(self):
144144
str_cases = [
145+
('path/to/file',
146+
('', '', 'path/to/file', '', '', ''),
147+
('', '', 'path/to/file', '', '')),
148+
('/path/to/file',
149+
('', '', '/path/to/file', '', '', ''),
150+
('', '', '/path/to/file', '', '')),
151+
('//path/to/file',
152+
('', 'path', '/to/file', '', '', ''),
153+
('', 'path', '/to/file', '', '')),
154+
('////path/to/file',
155+
('', '', '//path/to/file', '', '', ''),
156+
('', '', '//path/to/file', '', '')),
157+
('scheme:path/to/file',
158+
('scheme', '', 'path/to/file', '', '', ''),
159+
('scheme', '', 'path/to/file', '', '')),
160+
('scheme:/path/to/file',
161+
('scheme', '', '/path/to/file', '', '', ''),
162+
('scheme', '', '/path/to/file', '', '')),
163+
('scheme://path/to/file',
164+
('scheme', 'path', '/to/file', '', '', ''),
165+
('scheme', 'path', '/to/file', '', '')),
166+
('scheme:////path/to/file',
167+
('scheme', '', '//path/to/file', '', '', ''),
168+
('scheme', '', '//path/to/file', '', '')),
145169
('file:///tmp/junk.txt',
146170
('file', '', '/tmp/junk.txt', '', '', ''),
147171
('file', '', '/tmp/junk.txt', '', '')),

Lib/urllib/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def urlunsplit(components):
525525
empty query; the RFC states that these are equivalent)."""
526526
scheme, netloc, url, query, fragment, _coerce_result = (
527527
_coerce_args(*components))
528-
if netloc or (scheme and scheme in uses_netloc):
528+
if netloc or (scheme and scheme in uses_netloc) or url[:2] == '//':
529529
if url and url[:1] != '/': url = '/' + url
530530
url = '//' + (netloc or '') + url
531531
if scheme:
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Makes sure that file URIs with multiple leading slashes (file:////, etc.) are properly round-tripped by urllib.parse. Patch by Ashwin Ramaswami
1+
Fix :func:`urllib.parse.urlunparse` and :func:`urllib.parse.urlunsplit` for URIs with path starting with multiple slashes and no authority.
2+
Based on patch by Ashwin Ramaswami.

0 commit comments

Comments
 (0)