Skip to content

Commit 55a26de

Browse files
zoobabarneygaleeryksun
authored
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: Barney Gale <barney.gale@gmail.com> Co-authored-by: Eryk Sun <eryksun@gmail.com>
1 parent 7571764 commit 55a26de

File tree

4 files changed

+78
-37
lines changed

4 files changed

+78
-37
lines changed

Lib/ntpath.py

Lines changed: 22 additions & 29 deletions
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,28 +176,26 @@ def splitdrive(p):
172176
sep = b'\\'
173177
altsep = b'/'
174178
colon = b':'
179+
unc_prefix = b'\\\\?\\UNC\\'
175180
else:
176181
sep = '\\'
177182
altsep = '/'
178183
colon = ':'
184+
unc_prefix = '\\\\?\\UNC\\'
179185
normp = p.replace(altsep, sep)
180-
if (normp[0:2] == sep*2) and (normp[2:3] != sep):
181-
# is a UNC path:
182-
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
183-
# \\machine\mountpoint\directory\etc\...
184-
# directory ^^^^^^^^^^^^^^^
185-
index = normp.find(sep, 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
190+
index = normp.find(sep, start)
186191
if index == -1:
187-
return p[:0], p
192+
return p, p[:0]
188193
index2 = normp.find(sep, index + 1)
189-
# a UNC path can't have two slashes in a row
190-
# (after the initial two)
191-
if index2 == index + 1:
192-
return p[:0], p
193194
if index2 == -1:
194-
index2 = len(p)
195+
return p, p[:0]
195196
return p[:index2], p[index2:]
196197
if normp[1:2] == colon:
198+
# Drive-letter drives, e.g. X:
197199
return p[:2], p[2:]
198200
return p[:0], p
199201

@@ -499,20 +501,11 @@ def normpath(path):
499501
altsep = b'/'
500502
curdir = b'.'
501503
pardir = b'..'
502-
special_prefixes = (b'\\\\.\\', b'\\\\?\\')
503504
else:
504505
sep = '\\'
505506
altsep = '/'
506507
curdir = '.'
507508
pardir = '..'
508-
special_prefixes = ('\\\\.\\', '\\\\?\\')
509-
if path.startswith(special_prefixes):
510-
# in the case of paths with these prefixes:
511-
# \\.\ -> device names
512-
# \\?\ -> literal paths
513-
# do not do any normalization, but return the path
514-
# unchanged apart from the call to os.fspath()
515-
return path
516509
path = path.replace(altsep, sep)
517510
prefix, path = splitdrive(path)
518511

Lib/test/test_ntpath.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,50 @@ 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

121+
# gh-81790: support device namespace, including UNC drives.
122+
tester('ntpath.splitdrive("//?/c:")', ("//?/c:", ""))
123+
tester('ntpath.splitdrive("//?/c:/")', ("//?/c:", "/"))
124+
tester('ntpath.splitdrive("//?/c:/dir")', ("//?/c:", "/dir"))
125+
tester('ntpath.splitdrive("//?/UNC")', ("//?/UNC", ""))
126+
tester('ntpath.splitdrive("//?/UNC/")', ("//?/UNC/", ""))
127+
tester('ntpath.splitdrive("//?/UNC/server/")', ("//?/UNC/server/", ""))
128+
tester('ntpath.splitdrive("//?/UNC/server/share")', ("//?/UNC/server/share", ""))
129+
tester('ntpath.splitdrive("//?/UNC/server/share/dir")', ("//?/UNC/server/share", "/dir"))
130+
tester('ntpath.splitdrive("//?/VOLUME{00000000-0000-0000-0000-000000000000}/spam")',
131+
('//?/VOLUME{00000000-0000-0000-0000-000000000000}', '/spam'))
132+
tester('ntpath.splitdrive("//?/BootPartition/")', ("//?/BootPartition", "/"))
133+
134+
tester('ntpath.splitdrive("\\\\?\\c:")', ("\\\\?\\c:", ""))
135+
tester('ntpath.splitdrive("\\\\?\\c:\\")', ("\\\\?\\c:", "\\"))
136+
tester('ntpath.splitdrive("\\\\?\\c:\\dir")', ("\\\\?\\c:", "\\dir"))
137+
tester('ntpath.splitdrive("\\\\?\\UNC")', ("\\\\?\\UNC", ""))
138+
tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("\\\\?\\UNC\\", ""))
139+
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\")', ("\\\\?\\UNC\\server\\", ""))
140+
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share")', ("\\\\?\\UNC\\server\\share", ""))
141+
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share\\dir")',
142+
("\\\\?\\UNC\\server\\share", "\\dir"))
143+
tester('ntpath.splitdrive("\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}\\spam")',
144+
('\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}', '\\spam'))
145+
tester('ntpath.splitdrive("\\\\?\\BootPartition\\")', ("\\\\?\\BootPartition", "\\"))
146+
147+
# gh-96290: support partial/invalid UNC drives
148+
tester('ntpath.splitdrive("//")', ("//", "")) # empty server & missing share
149+
tester('ntpath.splitdrive("///")', ("///", "")) # empty server & empty share
150+
tester('ntpath.splitdrive("///y")', ("///y", "")) # empty server & non-empty share
151+
tester('ntpath.splitdrive("//x")', ("//x", "")) # non-empty server & missing share
152+
tester('ntpath.splitdrive("//x/")', ("//x/", "")) # non-empty server & empty share
153+
121154
def test_split(self):
122155
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
123156
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
@@ -136,6 +169,10 @@ def test_isabs(self):
136169
tester('ntpath.isabs("\\foo")', 1)
137170
tester('ntpath.isabs("\\foo\\bar")', 1)
138171

172+
# gh-96290: normal UNC paths and device paths without trailing backslashes
173+
tester('ntpath.isabs("\\\\conky\\mountpoint")', 1)
174+
tester('ntpath.isabs("\\\\.\\C:")', 1)
175+
139176
def test_commonprefix(self):
140177
tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
141178
"/home/swen")
@@ -245,6 +282,12 @@ def test_normpath(self):
245282
tester("ntpath.normpath('//server/share/../..')", '\\\\server\\share\\')
246283
tester("ntpath.normpath('//server/share/../../')", '\\\\server\\share\\')
247284

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

Lib/test/test_zipfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,10 +1468,10 @@ def test_extract_hackers_arcnames_windows_only(self):
14681468
(r'C:\foo\bar', 'foo/bar'),
14691469
(r'//conky/mountpoint/foo/bar', 'foo/bar'),
14701470
(r'\\conky\mountpoint\foo\bar', 'foo/bar'),
1471-
(r'///conky/mountpoint/foo/bar', 'conky/mountpoint/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'),
1471+
(r'///conky/mountpoint/foo/bar', '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'),
14751475
(r'//?/C:/foo/bar', 'foo/bar'),
14761476
(r'\\?\C:\foo\bar', 'foo/bar'),
14771477
(r'C:/../C:/foo/bar', 'C_/foo/bar'),
Lines changed: 5 additions & 0 deletions
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)