Skip to content

Commit 97b2cea

Browse files
gh-127217: Fix pathname2url() for paths starting with multiple slashes on Posix (GH-127218)
1 parent 2bb7846 commit 97b2cea

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/test/test_urllib.py

+3
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,9 @@ def test_pathname2url_posix(self):
14581458
fn = urllib.request.pathname2url
14591459
self.assertEqual(fn('/'), '/')
14601460
self.assertEqual(fn('/a/b.c'), '/a/b.c')
1461+
self.assertEqual(fn('//a/b.c'), '////a/b.c')
1462+
self.assertEqual(fn('///a/b.c'), '/////a/b.c')
1463+
self.assertEqual(fn('////a/b.c'), '//////a/b.c')
14611464
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
14621465

14631466
@unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')

Lib/urllib/request.py

+4
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,10 @@ def url2pathname(pathname):
16671667
def pathname2url(pathname):
16681668
"""OS-specific conversion from a file system path to a relative URL
16691669
of the 'file' scheme; not recommended for general use."""
1670+
if pathname[:2] == '//':
1671+
# Add explicitly empty authority to avoid interpreting the path
1672+
# as authority.
1673+
pathname = '//' + pathname
16701674
encoding = sys.getfilesystemencoding()
16711675
errors = sys.getfilesystemencodeerrors()
16721676
return quote(pathname, encoding=encoding, errors=errors)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`urllib.request.pathname2url` for paths starting with multiple
2+
slashes on Posix.

0 commit comments

Comments
 (0)