https://github.com/python/cpython/commit/97b2ceaaaf88a73a45254912a0e972412879ccbf
commit: 97b2ceaaaf88a73a45254912a0e972412879ccbf
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-11-24T19:30:29+02:00
summary:

gh-127217: Fix pathname2url() for paths starting with multiple slashes on Posix 
(GH-127218)

files:
A Misc/NEWS.d/next/Library/2024-11-24-12-41-31.gh-issue-127217.UAXGFr.rst
M Lib/test/test_urllib.py
M Lib/urllib/request.py

diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 22ef3c648e271d..fe16badc5bc77d 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1458,6 +1458,9 @@ def test_pathname2url_posix(self):
         fn = urllib.request.pathname2url
         self.assertEqual(fn('/'), '/')
         self.assertEqual(fn('/a/b.c'), '/a/b.c')
+        self.assertEqual(fn('//a/b.c'), '////a/b.c')
+        self.assertEqual(fn('///a/b.c'), '/////a/b.c')
+        self.assertEqual(fn('////a/b.c'), '//////a/b.c')
         self.assertEqual(fn('/a/b%#c'), '/a/b%25%23c')
 
     @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 80be65c613e971..9e555432688a5b 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1667,6 +1667,10 @@ def url2pathname(pathname):
     def pathname2url(pathname):
         """OS-specific conversion from a file system path to a relative URL
         of the 'file' scheme; not recommended for general use."""
+        if pathname[:2] == '//':
+            # Add explicitly empty authority to avoid interpreting the path
+            # as authority.
+            pathname = '//' + pathname
         encoding = sys.getfilesystemencoding()
         errors = sys.getfilesystemencodeerrors()
         return quote(pathname, encoding=encoding, errors=errors)
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-24-12-41-31.gh-issue-127217.UAXGFr.rst 
b/Misc/NEWS.d/next/Library/2024-11-24-12-41-31.gh-issue-127217.UAXGFr.rst
new file mode 100644
index 00000000000000..3139e33302f378
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-24-12-41-31.gh-issue-127217.UAXGFr.rst
@@ -0,0 +1,2 @@
+Fix :func:`urllib.request.pathname2url` for paths starting with multiple
+slashes on Posix.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to