Skip to content

Commit c711deb

Browse files
[3.13] gh-127217: Fix pathname2url() for paths starting with multiple slashes on Posix (GH-127218) (GH-127230)
(cherry picked from commit 97b2cea) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 4cba0e6 commit c711deb

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/test/test_urllib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,9 @@ def test_pathname2url_posix(self):
15561556
fn = urllib.request.pathname2url
15571557
self.assertEqual(fn('/'), '/')
15581558
self.assertEqual(fn('/a/b.c'), '/a/b.c')
1559+
self.assertEqual(fn('//a/b.c'), '////a/b.c')
1560+
self.assertEqual(fn('///a/b.c'), '/////a/b.c')
1561+
self.assertEqual(fn('////a/b.c'), '//////a/b.c')
15591562
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
15601563

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

Lib/urllib/request.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,10 @@ def url2pathname(pathname):
16701670
def pathname2url(pathname):
16711671
"""OS-specific conversion from a file system path to a relative URL
16721672
of the 'file' scheme; not recommended for general use."""
1673+
if pathname[:2] == '//':
1674+
# Add explicitly empty authority to avoid interpreting the path
1675+
# as authority.
1676+
pathname = '//' + pathname
16731677
encoding = sys.getfilesystemencoding()
16741678
errors = sys.getfilesystemencodeerrors()
16751679
return quote(pathname, encoding=encoding, errors=errors)
Lines changed: 2 additions & 0 deletions
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)