]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127217: Fix pathname2url() for paths starting with multiple slashes on Posix ...
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 24 Nov 2024 17:30:29 +0000 (19:30 +0200)
committerGitHub <noreply@github.com>
Sun, 24 Nov 2024 17:30:29 +0000 (19:30 +0200)
Lib/test/test_urllib.py
Lib/urllib/request.py
Misc/NEWS.d/next/Library/2024-11-24-12-41-31.gh-issue-127217.UAXGFr.rst [new file with mode: 0644]

index 22ef3c648e271dcab5ad2ae4b6aeb45a6b12f9ec..fe16badc5bc77d7cd2bea866f1380f94d86ce8f6 100644 (file)
@@ -1458,6 +1458,9 @@ class Pathname_Tests(unittest.TestCase):
         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')
index 80be65c613e9710c6f18d98689d55ceb367e24a2..9e555432688a5b1bb4295ce92eaac2eadab06b11 100644 (file)
@@ -1667,6 +1667,10 @@ else:
     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 (file)
index 0000000..3139e33
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`urllib.request.pathname2url` for paths starting with multiple
+slashes on Posix.