summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-11-24 17:30:29 (GMT)
committerGitHub <noreply@github.com>2024-11-24 17:30:29 (GMT)
commit97b2ceaaaf88a73a45254912a0e972412879ccbf (patch)
tree0d103bb0bf447a581c49142da72c8293f87394fb
parent2bb7846cacb342246aada5ed92d323e54c946063 (diff)
downloadcpython-97b2ceaaaf88a73a45254912a0e972412879ccbf.zip
cpython-97b2ceaaaf88a73a45254912a0e972412879ccbf.tar.gz
cpython-97b2ceaaaf88a73a45254912a0e972412879ccbf.tar.bz2
gh-127217: Fix pathname2url() for paths starting with multiple slashes on Posix (GH-127218)
-rw-r--r--Lib/test/test_urllib.py3
-rw-r--r--Lib/urllib/request.py4
-rw-r--r--Misc/NEWS.d/next/Library/2024-11-24-12-41-31.gh-issue-127217.UAXGFr.rst2
3 files changed, 9 insertions, 0 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 22ef3c6..fe16bad 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -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')
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 80be65c..9e55543 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -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
index 0000000..3139e33
--- /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.