https://github.com/python/cpython/commit/78f307f39e6d3f4ebb172140945ad2ec1ee10811
commit: 78f307f39e6d3f4ebb172140945ad2ec1ee10811
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: barneygale <[email protected]>
date: 2024-10-31T16:59:15Z
summary:

[3.12] GH-126205: Fix conversion of UNC paths to file URIs (GH-126208) (#126248)

GH-126205: Fix conversion of UNC paths to file URIs (GH-126208)

File URIs for Windows UNC paths should begin with two slashes, not four.
(cherry picked from commit 951cb2c369e8b8fb9f93662658391a53fd727787)

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

files:
A Misc/NEWS.d/next/Library/2024-10-30-20-45-17.gh-issue-126205.CHEmtx.rst
M Lib/nturl2path.py
M Lib/test/test_urllib.py

diff --git a/Lib/nturl2path.py b/Lib/nturl2path.py
index 61852aff58912d..6453f202c26d14 100644
--- a/Lib/nturl2path.py
+++ b/Lib/nturl2path.py
@@ -55,16 +55,11 @@ def pathname2url(p):
     if p[:4] == '\\\\?\\':
         p = p[4:]
         if p[:4].upper() == 'UNC\\':
-            p = '\\' + p[4:]
+            p = '\\\\' + p[4:]
         elif p[1:2] != ':':
             raise OSError('Bad path: ' + p)
     if not ':' in p:
         # No drive specifier, just convert slashes and quote the name
-        if p[:2] == '\\\\':
-        # path is something like \\host\path\on\remote\host
-        # convert this to ////host/path/on/remote/host
-        # (notice doubling of slashes at the start of the path)
-            p = '\\\\' + p
         components = p.split('\\')
         return urllib.parse.quote('/'.join(components))
     comp = p.split(':', maxsplit=2)
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 5a6d877f6a3094..0e1b13d31c9273 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1533,7 +1533,7 @@ def test_pathname2url_win(self):
         # Test special prefixes are correctly handled in pathname2url()
         fn = urllib.request.pathname2url
         self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir')
-        self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), 
'/server/share/dir')
+        self.assertEqual(fn('\\\\?\\unc\\server\\share\\dir'), 
'//server/share/dir')
         self.assertEqual(fn("C:"), '///C:')
         self.assertEqual(fn("C:\\"), '///C:')
         self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c')
@@ -1544,14 +1544,14 @@ def test_pathname2url_win(self):
         self.assertRaises(IOError, fn, "XX:\\")
         # No drive letter
         self.assertEqual(fn("\\folder\\test\\"), '/folder/test/')
-        self.assertEqual(fn("\\\\folder\\test\\"), '////folder/test/')
-        self.assertEqual(fn("\\\\\\folder\\test\\"), '/////folder/test/')
-        self.assertEqual(fn('\\\\some\\share\\'), '////some/share/')
-        self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '////some/share/a/b.c')
-        self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), 
'////some/share/a/b%25%23c%C3%A9')
+        self.assertEqual(fn("\\\\folder\\test\\"), '//folder/test/')
+        self.assertEqual(fn("\\\\\\folder\\test\\"), '///folder/test/')
+        self.assertEqual(fn('\\\\some\\share\\'), '//some/share/')
+        self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '//some/share/a/b.c')
+        self.assertEqual(fn('\\\\some\\share\\a\\b%#c\xe9'), 
'//some/share/a/b%25%23c%C3%A9')
         # 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)
diff --git 
a/Misc/NEWS.d/next/Library/2024-10-30-20-45-17.gh-issue-126205.CHEmtx.rst 
b/Misc/NEWS.d/next/Library/2024-10-30-20-45-17.gh-issue-126205.CHEmtx.rst
new file mode 100644
index 00000000000000..c92ffb75056606
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-30-20-45-17.gh-issue-126205.CHEmtx.rst
@@ -0,0 +1,2 @@
+Fix issue where :func:`urllib.request.pathname2url` generated URLs beginning
+with four slashes (rather than two) when given a Windows UNC path.

_______________________________________________
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