https://github.com/python/cpython/commit/8c98ed846a7d7e50c4cf06f823d94737144dcf6a
commit: 8c98ed846a7d7e50c4cf06f823d94737144dcf6a
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2024-11-22T04:12:50Z
summary:

GH-127078: `url2pathname()`: handle extra slash before UNC drive in URL path 
(#127132)

Decode a file URI like `file://///server/share` as a UNC path like
`\\server\share`. This form of file URI is created by software the simply
prepends `file:///` to any absolute Windows path.

files:
A Misc/NEWS.d/next/Library/2024-11-22-03-40-02.gh-issue-127078.gI_PaP.rst
M Lib/nturl2path.py
M Lib/test/test_urllib.py

diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py
index 3308ee7c1c784e..66092e4821a0ec 100644
--- a/Lib/nturl2path.py
+++ b/Lib/nturl2path.py
@@ -22,6 +22,9 @@ def url2pathname(url):
     elif url[:12] == '//localhost/':
         # Skip past 'localhost' authority.
         url = url[11:]
+    if url[:3] == '///':
+        # Skip past extra slash before UNC drive in URL path.
+        url = url[1:]
     # Windows itself uses ":" even in URLs.
     url = url.replace(':', '|')
     if not '|' in url:
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index e1c1d3170d9807..a204ef41c3ce90 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1492,7 +1492,7 @@ def test_url2pathname_win(self):
         # UNC paths
         self.assertEqual(fn('//server/path/to/file'), 
'\\\\server\\path\\to\\file')
         self.assertEqual(fn('////server/path/to/file'), 
'\\\\server\\path\\to\\file')
-        self.assertEqual(fn('/////server/path/to/file'), 
'\\\\\\server\\path\\to\\file')
+        self.assertEqual(fn('/////server/path/to/file'), 
'\\\\server\\path\\to\\file')
         # Localhost paths
         self.assertEqual(fn('//localhost/C:/path/to/file'), 
'C:\\path\\to\\file')
         self.assertEqual(fn('//localhost/C|/path/to/file'), 
'C:\\path\\to\\file')
diff --git 
a/Misc/NEWS.d/next/Library/2024-11-22-03-40-02.gh-issue-127078.gI_PaP.rst 
b/Misc/NEWS.d/next/Library/2024-11-22-03-40-02.gh-issue-127078.gI_PaP.rst
new file mode 100644
index 00000000000000..a84c06f3c7a273
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-22-03-40-02.gh-issue-127078.gI_PaP.rst
@@ -0,0 +1,2 @@
+Fix issue where :func:`urllib.request.url2pathname` failed to discard an
+extra slash before a UNC drive in the URL path on Windows.

_______________________________________________
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