gh-96290: Support partial/invalid UNC drives in ntpath.normpath() and splitdrive() (GH-100351)

This brings the Python implementation of `ntpath.normpath()` in line with the C implementation added in 99fcf15

Co-authored-by: Eryk Sun <eryksun@gmail.com>
This commit is contained in:
Barney Gale 2023-01-12 19:24:57 +00:00 committed by GitHub
parent eecd422d1b
commit 005e69403d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 46 deletions

View file

@ -107,13 +107,13 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
('//conky/mountpoint', '/foo/bar'))
tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
('', '\\\\\\conky\\mountpoint\\foo\\bar'))
('\\\\\\conky', '\\mountpoint\\foo\\bar'))
tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
('', '///conky/mountpoint/foo/bar'))
('///conky', '/mountpoint/foo/bar'))
tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
('', '\\\\conky\\\\mountpoint\\foo\\bar'))
('\\\\conky\\', '\\mountpoint\\foo\\bar'))
tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
('', '//conky//mountpoint/foo/bar'))
('//conky/', '/mountpoint/foo/bar'))
# Issue #19911: UNC part containing U+0130
self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'),
('//conky/MOUNTPOİNT', '/foo/bar'))
@ -121,8 +121,8 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.splitdrive("//?/c:")', ("//?/c:", ""))
tester('ntpath.splitdrive("//?/c:/")', ("//?/c:", "/"))
tester('ntpath.splitdrive("//?/c:/dir")', ("//?/c:", "/dir"))
tester('ntpath.splitdrive("//?/UNC")', ("", "//?/UNC"))
tester('ntpath.splitdrive("//?/UNC/")', ("", "//?/UNC/"))
tester('ntpath.splitdrive("//?/UNC")', ("//?/UNC", ""))
tester('ntpath.splitdrive("//?/UNC/")', ("//?/UNC/", ""))
tester('ntpath.splitdrive("//?/UNC/server/")', ("//?/UNC/server/", ""))
tester('ntpath.splitdrive("//?/UNC/server/share")', ("//?/UNC/server/share", ""))
tester('ntpath.splitdrive("//?/UNC/server/share/dir")', ("//?/UNC/server/share", "/dir"))
@ -133,8 +133,8 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.splitdrive("\\\\?\\c:")', ("\\\\?\\c:", ""))
tester('ntpath.splitdrive("\\\\?\\c:\\")', ("\\\\?\\c:", "\\"))
tester('ntpath.splitdrive("\\\\?\\c:\\dir")', ("\\\\?\\c:", "\\dir"))
tester('ntpath.splitdrive("\\\\?\\UNC")', ("", "\\\\?\\UNC"))
tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("", "\\\\?\\UNC\\"))
tester('ntpath.splitdrive("\\\\?\\UNC")', ("\\\\?\\UNC", ""))
tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("\\\\?\\UNC\\", ""))
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\")', ("\\\\?\\UNC\\server\\", ""))
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share")', ("\\\\?\\UNC\\server\\share", ""))
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share\\dir")',
@ -143,6 +143,13 @@ class TestNtpath(NtpathTestCase):
('\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}', '\\spam'))
tester('ntpath.splitdrive("\\\\?\\BootPartition\\")', ("\\\\?\\BootPartition", "\\"))
# gh-96290: support partial/invalid UNC drives
tester('ntpath.splitdrive("//")', ("//", "")) # empty server & missing share
tester('ntpath.splitdrive("///")', ("///", "")) # empty server & empty share
tester('ntpath.splitdrive("///y")', ("///y", "")) # empty server & non-empty share
tester('ntpath.splitdrive("//x")', ("//x", "")) # non-empty server & missing share
tester('ntpath.splitdrive("//x/")', ("//x/", "")) # non-empty server & empty share
def test_split(self):
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
@ -161,6 +168,10 @@ class TestNtpath(NtpathTestCase):
tester('ntpath.isabs("\\foo")', 1)
tester('ntpath.isabs("\\foo\\bar")', 1)
# gh-96290: normal UNC paths and device paths without trailing backslashes
tester('ntpath.isabs("\\\\conky\\mountpoint")', 1)
tester('ntpath.isabs("\\\\.\\C:")', 1)
def test_commonprefix(self):
tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
"/home/swen")
@ -270,6 +281,12 @@ class TestNtpath(NtpathTestCase):
tester("ntpath.normpath('//server/share/../..')", '\\\\server\\share\\')
tester("ntpath.normpath('//server/share/../../')", '\\\\server\\share\\')
# gh-96290: don't normalize partial/invalid UNC drives as rooted paths.
tester("ntpath.normpath('\\\\foo\\\\')", '\\\\foo\\\\')
tester("ntpath.normpath('\\\\foo\\')", '\\\\foo\\')
tester("ntpath.normpath('\\\\foo')", '\\\\foo')
tester("ntpath.normpath('\\\\')", '\\\\')
def test_realpath_curdir(self):
expected = ntpath.normpath(os.getcwd())
tester("ntpath.realpath('.')", expected)