bpo-43607: Fix urllib handling of Windows paths with \\?\ prefix (GH-25539)

This commit is contained in:
Steve Dower 2021-04-23 18:02:47 +01:00 committed by GitHub
parent 7d37b86ad4
commit 3513d55a61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View file

@ -50,6 +50,14 @@ def pathname2url(p):
# becomes # becomes
# ///C:/foo/bar/spam.foo # ///C:/foo/bar/spam.foo
import urllib.parse 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: if not ':' in p:
# No drive specifier, just convert slashes and quote the name # No drive specifier, just convert slashes and quote the name
if p[:2] == '\\\\': if p[:2] == '\\\\':
@ -59,7 +67,7 @@ def pathname2url(p):
p = '\\\\' + p p = '\\\\' + p
components = p.split('\\') components = p.split('\\')
return urllib.parse.quote('/'.join(components)) return urllib.parse.quote('/'.join(components))
comp = p.split(':') comp = p.split(':', maxsplit=2)
if len(comp) != 2 or len(comp[0]) > 1: if len(comp) != 2 or len(comp[0]) > 1:
error = 'Bad path: ' + p error = 'Bad path: ' + p
raise OSError(error) raise OSError(error)

View file

@ -1526,6 +1526,24 @@ class Pathname_Tests(unittest.TestCase):
"url2pathname() failed; %s != %s" % "url2pathname() failed; %s != %s" %
(expect, result)) (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', @unittest.skipUnless(sys.platform == 'win32',
'test specific to the urllib.url2path function.') 'test specific to the urllib.url2path function.')
def test_ntpath(self): def test_ntpath(self):

View file

@ -0,0 +1,2 @@
:mod:`urllib` can now convert Windows paths with ``\\?\`` prefixes into URL
paths.