10000 bpo-37609: Add device path support in ntpath splitdrive by nsiregar · Pull Request #14841 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

bpo-37609: Add device path support in ntpath splitdrive #14841

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

Closed
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
Next Next commit
bpo-37609: Add device path support in ntpath splitdrive
  • Loading branch information
nsiregar committed Aug 24, 2019
commit 6ac0014c2c310e01f93c1a9830bd19d5a64399bc
35 changes: 25 additions & 10 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,19 @@ def splitdrive(p):
"""
p = os.fspath(p)
if len(p) >= 2:
if isinstance(p, bytes):
sep = b'\\'
altsep = b'/'
colon = b':'
else:
sep = '\\'
altsep = '/'
colon = ':'
sep, altsep, colon = get_separator(p)
normp = p.replace(altsep, sep)
if (normp[0:2] == sep*2) and (normp[2:3] != sep):
if is_unc_path(normp, sep):
# is a UNC path:
# vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
# \\machine\mountpoint\directory\etc\...
# directory ^^^^^^^^^^^^^^^
index = normp.find(sep, 2)
if index == -1:
return p[:0], p
if is_extended_unc(normp, colon):
start = normp.find(sep, index + 1)
index = normp.find(sep, start + 1)
index2 = normp.find(sep, index + 1)
# a UNC path can't have two slashes in a row
# (after the initial two)
Expand All @@ -159,11 +155,30 @@ def splitdrive(p):
if index2 == -1:
index2 = len(p)
return p[:index2], p[index2:]
if normp[1:2] == colon:
if is_drive_path(normp, colon):
return p[:2], p[2:]
return p[:0], p


def is_unc_path(path, sep):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are clearly private (e.g. parameterized by sep and colon), so the function names should have a leading underscore.

return (path[0:2] == sep*2) and (path[2:3] != sep)


def is_drive_path(path, colon):
return path[1:2] == colon


def is_extended_unc(path, colon):
return path[2] in ['?', '.'] and path[-2] != colon


def get_separator(path):
sep, altsep, colon = ['\\', '/', ':']
if isinstance(path, bytes):
sep, altsep, colon = [b'\\', b'/', b':']
return sep, altsep, colon


# Split a path in head (everything up to the last '/') and tail (the
# rest). After the trailing '/' is stripped, the invariant
# join(head, tail) == p holds.
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def test_splitdrive(self):
# Issue #19911: UNC part containing U+0130
self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOİNT/foo/bar'),
('//conky/MOUNTPOİNT', '/foo/bar'))
# Issue #37609: UNC device path
self.assertEqual(ntpath.splitdrive('//?/UNC/localhost/C$/foo/bar'),
('//?/UNC/localhost/C$', '/foo/bar'))
self.assertEqual(ntpath.splitdrive('//./UNC/localhost/C$/foo/bar'),
('//./UNC/localhost/C$', '/foo/bar'))

def test_split(self):
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
Expand Down
0