Skip to content

Commit 5bfacd2

Browse files
[3.12] gh-127217: Fix pathname2url() for paths starting with multiple slashes on Posix (GH-127218) (GH-127231)
(cherry picked from commit 97b2cea) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent b52ab48 commit 5bfacd2

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
@@ -1565,6 +1565,9 @@ def test_pathname2url_posix(self):
15651565
fn = urllib.request.pathname2url
15661566
self.assertEqual(fn('/'), '/')
15671567
self.assertEqual(fn('/a/b.c'), '/a/b.c')
1568+
self.assertEqual(fn('//a/b.c'), '////a/b.c')
1569+
self.assertEqual(fn('///a/b.c'), '/////a/b.c')
1570+
self.assertEqual(fn('////a/b.c'), '//////a/b.c')
15681571
self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
15691572

15701573
@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
@@ -1695,6 +1695,10 @@ def url2pathname(pathname):
16951695
def pathname2url(pathname):
16961696
"""OS-specific conversion from a file system path to a relative URL
16971697
of the 'file' scheme; not recommended for general use."""
1698+
if pathname[:2] == '//':
1699+
# Add explicitly empty authority to avoid interpreting the path
1700+
# as authority.
1701+
pathname = '//' + pathname
16981702
encoding = sys.getfilesystemencoding()
16991703
errors = sys.getfilesystemencodeerrors()
17001704
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)