]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43607: Fix urllib handling of Windows paths with \\?\ prefix (GH-25539)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 23 Apr 2021 18:21:45 +0000 (11:21 -0700)
committerGitHub <noreply@github.com>
Fri, 23 Apr 2021 18:21:45 +0000 (19:21 +0100)
(cherry picked from commit 3513d55a617012002c3f82dbf3cec7ec1abd7090)

Co-authored-by: Steve Dower <steve.dower@python.org>
Lib/nturl2path.py
Lib/test/test_urllib.py
Misc/NEWS.d/next/Library/2021-04-22-22-39-58.bpo-43607.7IYDkG.rst [new file with mode: 0644]

index 853e6608380e92da93581c2a39cb1460b93bb9c7..61852aff58912d81f7969b64df5fd1ef2f06fd92 100644 (file)
@@ -50,6 +50,14 @@ def pathname2url(p):
     # becomes
     #   ///C:/foo/bar/spam.foo
     import urllib.parse
+    # First, clean up some special forms. We are going to sacrifice
+    # the additional information anyway
+    if p[:4] == '\\\\?\\':
+        p = p[4:]
+        if p[:4].upper() == 'UNC\\':
+            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] == '\\\\':
@@ -59,7 +67,7 @@ def pathname2url(p):
             p = '\\\\' + p
         components = p.split('\\')
         return urllib.parse.quote('/'.join(components))
-    comp = p.split(':')
+    comp = p.split(':', maxsplit=2)
     if len(comp) != 2 or len(comp[0]) > 1:
         error = 'Bad path: ' + p
         raise OSError(error)
index 8626687151185e3140516d592a31a3534739d928..b4e2f5fe00d6e079e2fe49b75d225b5668931f25 100644 (file)
@@ -1489,6 +1489,24 @@ class Pathname_Tests(unittest.TestCase):
                          "url2pathname() failed; %s != %s" %
                          (expect, result))
 
+    @unittest.skipUnless(sys.platform == 'win32',
+                         'test specific to the nturl2path functions.')
+    def test_prefixes(self):
+        # Test special prefixes are correctly handled in pathname2url()
+        given = '\\\\?\\C:\\dir'
+        expect = '///C:/dir'
+        result = urllib.request.pathname2url(given)
+        self.assertEqual(expect, result,
+                         "pathname2url() failed; %s != %s" %
+                         (expect, result))
+        given = '\\\\?\\unc\\server\\share\\dir'
+        expect = '/server/share/dir'
+        result = urllib.request.pathname2url(given)
+        self.assertEqual(expect, result,
+                         "pathname2url() failed; %s != %s" %
+                         (expect, result))
+
+
     @unittest.skipUnless(sys.platform == 'win32',
                          'test specific to the urllib.url2path function.')
     def test_ntpath(self):
diff --git a/Misc/NEWS.d/next/Library/2021-04-22-22-39-58.bpo-43607.7IYDkG.rst b/Misc/NEWS.d/next/Library/2021-04-22-22-39-58.bpo-43607.7IYDkG.rst
new file mode 100644 (file)
index 0000000..fa62846
--- /dev/null
@@ -0,0 +1,2 @@
+:mod:`urllib` can now convert Windows paths with ``\\?\`` prefixes into URL
+paths.