https://github.com/python/cpython/commit/04f38bb775e998e78bb0cb2747ef57f50f129cb5
commit: 04f38bb775e998e78bb0cb2747ef57f50f129cb5
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: barneygale <[email protected]>
date: 2024-11-14T23:52:46Z
summary:

[3.12] GH-126766: `url2pathname()`: handle empty authority section. (GH-126767) 
(#126837)

GH-126766: `url2pathname()`: handle empty authority section. (GH-126767)

Discard two leading slashes from the beginning of a `file:` URI if they
introduce an empty authority section. As a result, file URIs like
`///etc/hosts` are correctly parsed as `/etc/hosts`.
(cherry picked from commit cae9d9d20f61cdbde0765efa340b6b596c31b67f)

Co-authored-by: Barney Gale <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-11-12-21-43-12.gh-issue-126766.oi2KJ7.rst
M Lib/nturl2path.py
M Lib/test/test_urllib.py
M Lib/urllib/request.py

diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py
index 9ecabff21c33e1..255eb2f547c2ce 100644
--- a/Lib/nturl2path.py
+++ b/Lib/nturl2path.py
@@ -19,10 +19,9 @@ def url2pathname(url):
     url = url.replace(':', '|')
     if not '|' in url:
         # No drive specifier, just convert slashes
-        if url[:4] == '////':
-            # path is something like ////host/path/on/remote/host
-            # convert this to \\host\path\on\remote\host
-            # (notice halving of slashes at the start of the path)
+        if url[:3] == '///':
+            # URL has an empty authority section, so the path begins on the
+            # third character.
             url = url[2:]
         # make sure not to convert quoted slashes :-)
         return urllib.parse.unquote(url.replace('/', '\\'))
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 5433072bcda741..15a698cc10f217 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1558,7 +1558,7 @@ def test_pathname2url_win(self):
         self.assertEqual(fn('//?/unc/server/share/dir'), '//server/share/dir')
         # Round-tripping
         urls = ['///C:',
-                '///folder/test/',
+                '/folder/test/',
                 '///C:/foo/bar/spam.foo']
         for url in urls:
             self.assertEqual(fn(urllib.request.url2pathname(url)), url)
@@ -1582,7 +1582,7 @@ def test_url2pathname_win(self):
         self.assertEqual(fn('/C|//'), 'C:\\\\')
         self.assertEqual(fn('///C|/path'), 'C:\\path')
         # No DOS drive
-        self.assertEqual(fn("///C/test/"), '\\\\\\C\\test\\')
+        self.assertEqual(fn("///C/test/"), '\\C\\test\\')
         self.assertEqual(fn("////C/test/"), '\\\\C\\test\\')
         # DOS drive paths
         self.assertEqual(fn('C:/path/to/file'), 'C:\\path\\to\\file')
@@ -1606,7 +1606,7 @@ def test_url2pathname_win(self):
         self.assertEqual(fn('//server/share/foo%2fbar'), 
'\\\\server\\share\\foo/bar')
         # Round-tripping
         paths = ['C:',
-                 r'\\\C\test\\',
+                 r'\C\test\\',
                  r'C:\foo\bar\spam.foo']
         for path in paths:
             self.assertEqual(fn(urllib.request.pathname2url(path)), path)
@@ -1617,8 +1617,8 @@ def test_url2pathname_posix(self):
         fn = urllib.request.url2pathname
         self.assertEqual(fn('/foo/bar'), '/foo/bar')
         self.assertEqual(fn('//foo/bar'), '//foo/bar')
-        self.assertEqual(fn('///foo/bar'), '///foo/bar')
-        self.assertEqual(fn('////foo/bar'), '////foo/bar')
+        self.assertEqual(fn('///foo/bar'), '/foo/bar')
+        self.assertEqual(fn('////foo/bar'), '//foo/bar')
         self.assertEqual(fn('//localhost/foo/bar'), '//localhost/foo/bar')
 
 class Utility_Tests(unittest.TestCase):
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 7228a35534b638..178c9795e19c6e 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1681,6 +1681,10 @@ def data_open(self, req):
     def url2pathname(pathname):
         """OS-specific conversion from a relative URL of the 'file' scheme
         to a file system path; not recommended for general use."""
+        if pathname[:3] == '///':
+            # URL has an empty authority section, so the path begins on the
+            # third character.
+            pathname = pathname[2:]
         return unquote(pathname)
 
     def pathname2url(pathname):
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-12-21-43-12.gh-issue-126766.oi2KJ7.rst 
b/Misc/NEWS.d/next/Library/2024-11-12-21-43-12.gh-issue-126766.oi2KJ7.rst
new file mode 100644
index 00000000000000..e3936305164883
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-12-21-43-12.gh-issue-126766.oi2KJ7.rst
@@ -0,0 +1,2 @@
+Fix issue where :func:`urllib.request.url2pathname` failed to discard two
+leading slashes introducing an empty authority section.

_______________________________________________
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