10000 gh-81790: support "UNC" device paths in `ntpath.splitdrive()` by barneygale · Pull Request #91882 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-81790: support "UNC" device paths in ntpath.splitdrive() #91882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 10, 2022
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
Revise implementation to affect only paths beginning \\?\UNC\.
  • Loading branch information
barneygale committed May 28, 2022
commit a7fa62b5055f122d57f04ffaf5025186b4b85dea
60 changes: 20 additions & 40 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,55 +146,35 @@ def splitdrive(p):
sep = b'\\'
altsep = b'/'
colon = b':'
prefix = b'\\\\?\\'
unc_prefix = b'UNC\\'
vol_prefix = b'VOLUME{'
unc_prefix = b'\\\\?\\UNC\\'
else:
sep = '\\'
altsep = '/'
colon = ':'
prefix = '\\\\?\\'
unc_prefix = 'UNC\\'
vol_prefix = 'VOLUME{'
unc_prefix = '\\\\?\\UNC\\'
normp = p.replace(altsep, sep)
if normp[:4] == prefix:
if normp[4:8].upper() == unc_prefix:
# e.g. \\?\UNC\server\share\dir\file
if (normp[0:2] == sep * 2) and (normp[2:3] != sep):
# is a UNC path:
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
# \\machine\mountpoint\directory\etc\...
# directory ^^^^^^^^^^^^^^^
if normp[:8].upper() == unc_prefix:
start = 8
elif normp[4:11].upper() == vol_prefix:
# e.g. \\?\VOLUME{...}\dir\file
start = 2
elif normp[5:6] == colon and not normp[6:7].strip(sep):
# e.g. \\?\c:\dir\file
return p[:6], p[6:]
else:
# anything else with a \\?\ prefix
return p[:0], p
else:
if normp[:2] == sep*2 and normp[2:3] != sep:
# e.g. \\server\share\dir\file
start = 2
elif normp[1:2] == colon:
# e.g. c:\dir\file
return p[:2], p[2:]
else:
# anything else
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
# 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:]
if index2 == -1:
index2 = len(p)
return p[:index2], p[index2:]
if normp[1:2] == colon:
return p[:2], p[2:]
return p[:0], p


Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,29 @@ 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(&q DE58 uot;//?/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("//?/VOLUME{00000000-0000-0000-0000-000000000000}/spam")',
('//?/VOLUME{00000000-0000-0000-0000-000000000000}', '/spam'))
tester('ntpath.splitdrive("//?/BootPartition/")', ("//?/BootPartition", "/"))
tester('ntpath.splitdrive("//?/Harddisk0Partition2/")', ("//?/Harddisk0Partition2", "/"))
tester('ntpath.splitdrive("//?/HarddiskVolume2/")', ("//?/HarddiskVolume2", "/"))

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("\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}\\spam")',
('\\\\?\\VOLUME{00000000-0000-0000-0000-000000000000}', '\\spam'))
tester('ntpath.splitdrive("\\\\?\\BootPartition\\")', ("\\\\?\\BootPartition", "\\"))
tester('ntpath.splitdrive("\\\\?\\Harddisk0Partition2\\")', ("\\\\?\\Harddisk0Partition2", "\\"))
tester('ntpath.splitdrive("\\\\?\\HarddiskVolume2\\")', ("\\\\?\\HarddiskVolume2", "\\"))

def test_split(self):
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
:func:`os.path.splitdrive` now understands DOS device paths (beginning
``\\?\``), including UNC links (beginning ``\\?\UNC\``). Contributed by
Barney Gale.
:func:`os.path.splitdrive` now understands DOS device paths with UNC
links (beginning ``\\?\UNC\``). Contributed by Barney Gale.
0