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

File URIs for Windows UNC paths should begin with two slashes, not four.
This commit is contained in:
Barney Gale 2024-10-30 22:56:58 +00:00 committed by GitHub
parent 2b2d607095
commit 951cb2c369
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 13 deletions

View File

@ -55,16 +55,11 @@ def pathname2url(p):
if p[:4] == '\\\\?\\': if p[:4] == '\\\\?\\':
p = p[4:] p = p[4:]
if p[:4].upper() == 'UNC\\': if p[:4].upper() == 'UNC\\':
p = '\\' + p[4:] p = '\\\\' + p[4:]
elif p[1:2] != ':': elif p[1:2] != ':':
raise OSError('Bad path: ' + p) 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] == '\\\\':
# 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('\\') components = p.split('\\')
return urllib.parse.quote('/'.join(components)) return urllib.parse.quote('/'.join(components))
comp = p.split(':', maxsplit=2) comp = p.split(':', maxsplit=2)

View File

@ -1524,7 +1524,7 @@ class Pathname_Tests(unittest.TestCase):
# Test special prefixes are correctly handled in pathname2url() # Test special prefixes are correctly handled in pathname2url()
fn = urllib.request.pathname2url fn = urllib.request.pathname2url
self.assertEqual(fn('\\\\?\\C:\\dir'), '///C:/dir') 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:\\"), '///C:') self.assertEqual(fn("C:\\"), '///C:')
self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c') self.assertEqual(fn('C:\\a\\b.c'), '///C:/a/b.c')
@ -1535,14 +1535,14 @@ class Pathname_Tests(unittest.TestCase):
self.assertRaises(IOError, fn, "XX:\\") self.assertRaises(IOError, fn, "XX:\\")
# No drive letter # 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("\\\\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\\'), '//some/share/')
self.assertEqual(fn('\\\\some\\share\\a\\b.c'), '////some/share/a/b.c') 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('\\\\some\\share\\a\\b%#c\xe9'), '//some/share/a/b%25%23c%C3%A9')
# Round-tripping # Round-tripping
urls = ['///C:', urls = ['///C:',
'/////folder/test/', '///folder/test/',
'///C:/foo/bar/spam.foo'] '///C:/foo/bar/spam.foo']
for url in urls: for url in urls:
self.assertEqual(fn(urllib.request.url2pathname(url)), url) self.assertEqual(fn(urllib.request.url2pathname(url)), url)

View File

@ -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.