Skip to content

Commit 005e694

Browse files
barneygaleeryksun
andauthored
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>
1 parent eecd422 commit 005e694

File tree

4 files changed

+55
-46
lines changed

4 files changed

+55
-46
lines changed

Diff for: Lib/ntpath.py

+21-34
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,20 @@ def normcase(s):
8787
def isabs(s):
8888
"""Test whether a path is absolute"""
8989
s = os.fspath(s)
90-
# Paths beginning with \\?\ are always absolute, but do not
91-
# necessarily contain a drive.
9290
if isinstance(s, bytes):
93-
if s.replace(b'/', b'\\').startswith(b'\\\\?\\'):
94-
return True
91+
sep = b'\\'
92+
altsep = b'/'
93+
colon_sep = b':\\'
9594
else:
96-
if s.replace('/', '\\').startswith('\\\\?\\'):
97-
return True
98-
s = splitdrive(s)[1]
99-
return len(s) > 0 and s[0] and s[0] in _get_bothseps(s)
95+
sep = '\\'
96+
altsep = '/'
97+
colon_sep = ':\\'
98+
s = s[:3].replace(altsep, sep)
99+
# Absolute: UNC, device, and paths with a drive and root.
100+
# LEGACY BUG: isabs("/x") should be false since the path has no drive.
101+
if s.startswith(sep) or s.startswith(colon_sep, 1):
102+
return True
103+
return False
100104

101105

102106
# Join two (or more) paths.
@@ -172,34 +176,26 @@ def splitdrive(p):
172176
sep = b'\\'
173177
altsep = b'/'
174178
colon = b':'
175-
unc_prefix = b'\\\\?\\UNC'
179+
unc_prefix = b'\\\\?\\UNC\\'
176180
else:
177181
sep = '\\'
178182
altsep = '/'
179183
colon = ':'
180-
unc_prefix = '\\\\?\\UNC'
184+
unc_prefix = '\\\\?\\UNC\\'
181185
normp = p.replace(altsep, sep)
182-
if (normp[0:2] == sep*2) and (normp[2:3] != sep):
183-
# is a UNC path:
184-
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
185-
# \\machine\mountpoint\directory\etc\...
186-
# directory ^^^^^^^^^^^^^^^
187-
if normp[:8].upper().rstrip(sep) == unc_prefix:
188-
start = 8
189-
else:
190-
start = 2
186+
if normp[0:2] == sep * 2:
187+
# UNC drives, e.g. \\server\share or \\?\UNC\server\share
188+
# Device drives, e.g. \\.\device or \\?\device
189+
start = 8 if normp[:8].upper() == unc_prefix else 2
191190
index = normp.find(sep, start)
192191
if index == -1:
193-
return p[:0], p
192+
return p, p[:0]
194193
index2 = normp.find(sep, index + 1)
195-
# a UNC path can't have two slashes in a row
196-
# (after the initial two)
197-
if index2 == index + 1:
198-
return p[:0], p
199194
if index2 == -1:
200-
index2 = len(p)
195+
return p, p[:0]
201196
return p[:index2], p[index2:]
202197
if normp[1:2] == colon:
198+
# Drive-letter drives, e.g. X:
203199
return p[:2], p[2:]
204200
return p[:0], p
205201

@@ -523,20 +519,11 @@ def normpath(path):
523519
altsep = b'/'
524520
curdir = b'.'
525521
pardir = b'..'
526-
special_prefixes = (b'\\\\.\\', b'\\\\?\\')
527522
else:
528523
sep = '\\'
529524
altsep = '/'
530525
curdir = '.'
531526
pardir = '..'
532-
special_prefixes = ('\\\\.\\', '\\\\?\\')
533-
if path.startswith(special_prefixes):
534-
# in the case of paths with these prefixes:
535-
# \\.\ -> device names
536-
# \\?\ -> literal paths
537-
# do not do any normalization, but return the path
538-
# unchanged apart from the call to os.fspath()
539-
return path
540527
path = path.replace(altsep, sep)
541528
prefix, path = splitdrive(path)
542529

Diff for: Lib/test/test_ntpath.py

+25-8
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 & non-empty share
150+
tester('ntpath.splitdrive("//x")', ("//x", "")) # non-empty server & missing share
151+
tester('ntpath.splitdrive("//x/")', ("//x/", "")) # non-empty 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")',
@@ -161,6 +168,10 @@ def test_isabs(self):
161168
tester('ntpath.isabs("\\foo")', 1)
162169
tester('ntpath.isabs("\\foo\\bar")', 1)
163170

171+
# gh-96290: normal UNC paths and device paths without trailing backslashes
172+
tester('ntpath.isabs("\\\\conky\\mountpoint")', 1)
173+
tester('ntpath.isabs("\\\\.\\C:")', 1)
174+
164175
def test_commonprefix(self):
165176
tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
166177
"/home/swen")
@@ -270,6 +281,12 @@ def test_normpath(self):
270281
tester("ntpath.normpath('//server/share/../..')", '\\\\server\\share\\')
271282
tester("ntpath.normpath('//server/share/../../')", '\\\\server\\share\\')
272283

284+
# gh-96290: don't normalize partial/invalid UNC drives as rooted paths.
285+
tester("ntpath.normpath('\\\\foo\\\\')", '\\\\foo\\\\')
286+
tester("ntpath.normpath('\\\\foo\\')", '\\\\foo\\')
287+
tester("ntpath.normpath('\\\\foo')", '\\\\foo')
288+
tester("ntpath.normpath('\\\\')", '\\\\')
289+
273290
def test_realpath_curdir(self):
274291
expected = ntpath.normpath(os.getcwd())
275292
tester("ntpath.realpath('.')", expected)

Diff for: Lib/test/test_zipfile/test_core.py

+4-4
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'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix handling of partial and invalid UNC drives in ``ntpath.splitdrive()``, and in
2+
``ntpath.normpath()`` on non-Windows systems. Paths such as '\\server' and '\\' are now considered
3+
by ``splitdrive()`` to contain only a drive, and consequently are not modified by ``normpath()`` on
4+
non-Windows systems. The behaviour of ``normpath()`` on Windows systems is unaffected, as native
5+
OS APIs are used. Patch by Eryk Sun, with contributions by Barney Gale.

0 commit comments

Comments
 (0)