gh-96290: support partial/invalid UNC drives in normpath() and splitd… · python/cpython@7ab642b · GitHub
Skip to content

Commit 7ab642b

Browse files
barneygaleeryksun
andcommitted
gh-96290: support partial/invalid UNC drives in normpath() and splitdrive()
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>
1 parent 797edb2 commit 7ab642b

5 files changed

Lines changed: 74 additions & 70 deletions

File tree

Lib/ntpath.py

Lines changed: 37 additions & 51 deletions

Lib/test/test_ntpath.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ def test_splitdrive(self):
107107
tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
108108
('//conky/mountpoint', '/foo/bar'))
109109
tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
110-
('', '\\\\\\conky\\mountpoint\\foo\\bar'))
110+
('\\\\\\conky', '\\mountpoint\\foo\\bar'))
111111
tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
112-
('', '///conky/mountpoint/foo/bar'))
112+
('///conky', '/mountpoint/foo/bar'))
113113
tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
114-
('', '\\\\conky\\\\mountpoint\\foo\\bar'))
114+
('\\\\conky\\', '\\mountpoint\\foo\\bar'))
115115
tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
116-
('', '//conky//mountpoint/foo/bar'))
116+
('//conky/', '/mountpoint/foo/bar'))
117117
# Issue #19911: UNC part containing U+0130
118118
self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'),
119119
('//conky/MOUNTPOİNT', '/foo/bar'))
120120
# gh-81790: support device namespace, including UNC drives.
121121
tester('ntpath.splitdrive("//?/c:")', ("//?/c:", ""))
122122
tester('ntpath.splitdrive("//?/c:/")', ("//?/c:", "/"))
123123
tester('ntpath.splitdrive("//?/c:/dir")', ("//?/c:", "/dir"))
124-
tester('ntpath.splitdrive("//?/UNC")', ("", "//?/UNC"))
125-
tester('ntpath.splitdrive("//?/UNC/")', ("", "//?/UNC/"))
124+
tester('ntpath.splitdrive("//?/UNC")', ("//?/UNC", ""))
125+
tester('ntpath.splitdrive("//?/UNC/")', ("//?/UNC/", ""))
126126
tester('ntpath.splitdrive("//?/UNC/server/")', ("//?/UNC/server/", ""))
127127
tester('ntpath.splitdrive("//?/UNC/server/share")', ("//?/UNC/server/share", ""))
128128
tester('ntpath.splitdrive("//?/UNC/server/share/dir")', ("//?/UNC/server/share", "/dir"))
@@ -133,8 +133,8 @@ def test_splitdrive(self):
133133
tester('ntpath.splitdrive("\\\\?\\c:")', ("\\\\?\\c:", ""))
134134
tester('ntpath.splitdrive("\\\\?\\c:\\")', ("\\\\?\\c:", "\\"))
135135
tester('ntpath.splitdrive("\\\\?\\c:\\dir")', ("\\\\?\\c:", "\\dir"))
136-
tester('ntpath.splitdrive("\\\\?\\UNC")', ("", "\\\\?\\UNC"))
137-
tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("", "\\\\?\\UNC\\"))
136+
tester('ntpath.splitdrive("\\\\?\\UNC")', ("\\\\?\\UNC", ""))
137+
tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("\\\\?\\UNC\\", ""))
138138
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\")', ("\\\\?\\UNC\\server\\", ""))
139139
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share")', ("\\\\?\\UNC\\server\\share", ""))
140140
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share\\dir")',
@@ -143,6 +143,13 @@ def test_splitdrive(self):
143143
('\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}', '\\spam'))
144144
tester('ntpath.splitdrive("\\\\?\\BootPartition\\")', ("\\\\?\\BootPartition", "\\"))
145145

146+
# gh-96290: support partial/invalid UNC drives
147+
tester('ntpath.splitdrive("//")', ("//", "")) # empty server & missing share
148+
tester('ntpath.splitdrive("///")', ("///", "")) # empty server & empty share
149+
tester('ntpath.splitdrive("///y")', ("///y", "")) # empty server & valid share
150+
tester('ntpath.splitdrive("//x")', ("//x", "")) # valid server & missing share
151+
tester('ntpath.splitdrive("//x/")', ("//x/", "")) # valid server & empty share
152+
146153
def test_split(self):
147154
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
148155
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
@@ -270,6 +277,13 @@ def test_normpath(self):
270277
tester("ntpath.normpath('//server/share/../..')", '\\\\server\\share\\')
271278
tester("ntpath.normpath('//server/share/../../')", '\\\\server\\share\\')
272279

280+
# gh-96290: don't normalize partial/invalid UNC drives
281+
tester("ntpath.normpath('\\\\foo\\bar')", '\\\\foo\\bar')
282+
tester("ntpath.normpath('\\\\foo\\\\')", '\\\\foo\\\\')
283+
tester("ntpath.normpath('\\\\foo\\')", '\\\\foo\\')
284+
tester("ntpath.normpath('\\\\foo')", '\\\\foo')
285+
tester("ntpath.normpath('\\\\')", '\\\\')
286+
273287
def test_realpath_curdir(self):
274288
expected = ntpath.normpath(os.getcwd())
275289
tester("ntpath.realpath('.')", expected)

Lib/test/test_pathlib.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,18 @@ def test_splitroot(self):
151151
self.assertEqual(f('c:a\\b'), ('c:', '', 'a\\b'))
152152
self.assertEqual(f('c:\\a\\b'), ('c:', '\\', 'a\\b'))
153153
# Redundant slashes in the root are collapsed.
154-
self.assertEqual(f('\\\\a'), ('', '\\', 'a'))
155-
self.assertEqual(f('\\\\\\a/b'), ('', '\\', 'a/b'))
156154
self.assertEqual(f('c:\\\\a'), ('c:', '\\', 'a'))
157155
self.assertEqual(f('c:\\\\\\a/b'), ('c:', '\\', 'a/b'))
158156
# Valid UNC paths.
159157
self.assertEqual(f('\\\\a\\b'), ('\\\\a\\b', '\\', ''))
160158
self.assertEqual(f('\\\\a\\b\\'), ('\\\\a\\b', '\\', ''))
161159
self.assertEqual(f('\\\\a\\b\\c\\d'), ('\\\\a\\b', '\\', 'c\\d'))
162-
# These are non-UNC paths (according to ntpath.py and test_ntpath).
163-
# However, command.com says such paths are invalid, so it's
160+
# Invalid or partial UNC paths. Per gh-96290, it's
164161
# difficult to know what the right semantics are.
165-
self.assertEqual(f('\\\\\\a\\b'), ('', '\\', 'a\\b'))
166-
self.assertEqual(f('\\\\a'), ('', '\\', 'a'))
162+
self.assertEqual(f('\\\\a'), ('\\\\a', '\\', ''))
163+
self.assertEqual(f('\\\\\\a/b'), ('\\\\\\a', '\\', '/b'))
164+
self.assertEqual(f('\\\\\\a\\b'), ('\\\\\\a', '\\', 'b'))
165+
self.assertEqual(f('\\\\a'), ('\\\\a', '\\', ''))
167166

168167

169168
#
@@ -182,7 +181,7 @@ class _BasePurePathTest(object):
182181
('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''),
183182
],
184183
'/b/c/d': [
185-
('a', '/b/c', 'd'), ('a', '///b//c', 'd/'),
184+
('a', '/b/c', 'd'),
186185
('/a', '/b/c', 'd'),
187186
# Empty components get removed.
188187
('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'),

Lib/test/test_zipfile/test_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,10 +1469,10 @@ def test_extract_hackers_arcnames_windows_only(self):
14691469
(r'C:\foo\bar', 'foo/bar'),
14701470
(r'//conky/mountpoint/foo/bar', 'foo/bar'),
14711471
(r'\\conky\mountpoint\foo\bar', 'foo/bar'),
1472-
(r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
1473-
(r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
1474-
(r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
1475-
(r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
1472+
(r'///conky/mountpoint/foo/bar', 'mountpoint/foo/bar'),
1473+
(r'\\\conky\mountpoint\foo\bar', 'mountpoint/foo/bar'),
1474+
(r'//conky//mountpoint/foo/bar', 'mountpoint/foo/bar'),
1475+
(r'\\conky\\mountpoint\foo\bar', 'mountpoint/foo/bar'),
14761476
(r'//?/C:/foo/bar', 'foo/bar'),
14771477
(r'\\?\C:\foo\bar', 'foo/bar'),
14781478
(r'C:/../C:/foo/bar', 'C_/foo/bar'),
Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)