]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-127217: Fix pathname2url() for paths starting with multiple slashes on...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 24 Nov 2024 18:13:23 +0000 (19:13 +0100)
committerGitHub <noreply@github.com>
Sun, 24 Nov 2024 18:13:23 +0000 (20:13 +0200)
(cherry picked from commit 97b2ceaaaf88a73a45254912a0e972412879ccbf)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
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 4bdbc19c1ffb7a46a534bab947f4c8a7db32af49..0710950e25a7cda99bb54986ba724c989e765ba7 100644 (file)
@@ -1565,6 +1565,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 c521d96d9538833801412a11dfced71684e5547e..9a559f44152be5e48196002e11ab41a434316f91 100644 (file)
@@ -1695,6 +1695,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.