Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address review feedback
  • Loading branch information
barneygale committed Apr 25, 2022
commit 264d79e3429223a637472d96daf2ff572038c842
52 changes: 28 additions & 24 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,40 +146,44 @@ def splitdrive(p):
sep = b'\\'
altsep = b'/'
colon = b':'
dev_prefix = b'\\\\?\\'
prefix = b'\\\\?\\'
unc_prefix = b'UNC\\'
else:
sep = '\\'
altsep = '/'
colon = ':'
dev_prefix = '\\\\?\\'
prefix = '\\\\?\\'
unc_prefix = 'UNC\\'
normp = p.replace(altsep, sep)
start = 0
if normp[:4] == dev_prefix:
start = 4
if normp[:4] == prefix:
if normp[4:8] == unc_prefix:
# prepend slashes to machine name
normp = 8 * sep + normp[8:]
start = 6
if (normp[start:start + 2] == sep*2) and (normp[start + 2:start + 3] != sep):
# is a UNC path:
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
# \\machine\mountpoint\directory\etc\...
# directory ^^^^^^^^^^^^^^^
index = normp.find(sep, start + 2)
if index == -1:
start = 8
elif normp[5:6] == colon and not normp[6:7].strip(sep):
return p[:6], p[6:]
else:
return p[:0], p
index2 = normp.find(sep, index + 1)
# a UNC path can't have two slashes in a row
# (after the initial two)
if index2 == index + 1:
else:
if normp[:2] == sep*2 and normp[2:3] != sep:
start = 2
elif normp[1:2] == colon:
return p[:2], p[2:]
else:
return p[:0], p
if index2 == -1:
index2 = len(p)
return p[:index2], p[index2:]
if normp[start + 1:start + 2] == colon:
return p[:start + 2], p[start + 2:]
# is a UNC path:
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
# \\machine\mountpoint\directory\etc\...
# directory ^^^^^^^^^^^^^^^
index = normp.find(sep, start)
if index == -1:
return p[:0], p
index2 = normp.find(sep, index + 1)
# a UNC path can't have two slashes in a row
# (after the initial two)
if index2 == index + 1:
return p[:0], p
if index2 == -1:
index2 = len(p)
return p[:index2], p[index2:]
return p[:0], p


Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ def test_splitdrive(self):
tester('ntpath.splitdrive("//?/c:")', ("//?/c:", ""))
tester('ntpath.splitdrive("//?/c:/")', ("//?/c:", "/"))
tester('ntpath.splitdrive("//?/c:/dir")', ("//?/c:", "/dir"))
tester('ntpath.splitdrive("//?/c:blah")', ("", "//?/c:blah"))
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"))
tester('ntpath.splitdrive("\\\\?\\c:")', ("\\\\?\\c:", ""))
tester('ntpath.splitdrive("\\\\?\\c:\\")', ("\\\\?\\c:", "\\"))
tester('ntpath.splitdrive("\\\\?\\c:\\dir")', ("\\\\?\\c:", "\\dir"))
tester('ntpath.splitdrive("\\\\?\\c:blah")', ("", "\\\\?\\c:blah"))
tester('ntpath.splitdrive("\\\\?\\UNC\\")', ("", "\\\\?\\UNC\\"))
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\")', ("\\\\?\\UNC\\server\\", ""))
tester('ntpath.splitdrive("\\\\?\\UNC\\server\\share")', ("\\\\?\\UNC\\server\\share", ""))
Expand Down