Skip to content
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

gh-102511: Add C implementation of os.path.splitroot() #118089

Merged
merged 29 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c397a23
Add C implementation of `ntpath.splitroot()`
nineteendo Apr 19, 2024
4bd9734
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 19, 2024
0177f70
Follow Pep 7
nineteendo Apr 19, 2024
e9cdec5
Merge branch 'speedup-os.path.splitroot' of https://github.com/ninete…
nineteendo Apr 19, 2024
bb816e9
Fix memory leak
nineteendo Apr 19, 2024
d00131a
Add C implementation of `posixpath.splitroot()`
nineteendo Apr 20, 2024
a4e5d15
Revert newlines
nineteendo Apr 20, 2024
7e1433c
Use `_Py_splitroot()`
nineteendo Apr 20, 2024
5491152
Rename to `_path_splitroot` replacing old one
nineteendo Apr 20, 2024
2244d3f
Update Misc/NEWS.d/next/Core and Builtins/2024-04-19-08-50-48.gh-issu…
nineteendo Apr 22, 2024
ca0761a
Rename old function to `_path_splitanchor`
nineteendo Apr 22, 2024
e1f32e9
Fix header
nineteendo Apr 22, 2024
34d4d90
Direct C call
nineteendo Apr 22, 2024
30d613b
Allow embedded null
nineteendo Apr 23, 2024
f78bad0
Fix segmentation fault
nineteendo Apr 23, 2024
5635da5
Fix redefinition
nineteendo Apr 23, 2024
6f62c1f
Python wrapper
nineteendo Apr 23, 2024
2e1b11a
Revert allow embedded null
nineteendo Apr 23, 2024
92d1c95
Revert newline
nineteendo Apr 23, 2024
5d35720
cast constant
nineteendo Apr 23, 2024
bb9b34d
Decrement ref counter
nineteendo Apr 23, 2024
bb64b18
Simplify exception clause
nineteendo Apr 23, 2024
75e3a70
Remove cast
nineteendo Apr 24, 2024
ef0ce7f
Remove fallback
nineteendo Apr 24, 2024
df9f974
Update Modules/posixmodule.c
nineteendo Apr 24, 2024
9cd7951
Update Modules/posixmodule.c
nineteendo Apr 24, 2024
f87f82b
Update Python/fileutils.c
nineteendo Apr 24, 2024
62a42fb
Follow pep 7
nineteendo Apr 24, 2024
6a74f15
Update Modules/posixmodule.c
nineteendo Apr 24, 2024
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
2 changes: 2 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ extern wchar_t *_Py_normpath_and_size(wchar_t *path, Py_ssize_t size, Py_ssize_t
extern HRESULT PathCchSkipRoot(const wchar_t *pszPath, const wchar_t **ppszRootEnd);
#endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */

extern void _Py_skiproot(wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize);

// Macros to protect CRT calls against instant termination when passed an
// invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
// Usage:
Expand Down
28 changes: 15 additions & 13 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,29 @@ def _path_join(*path_parts):
return ""
if len(path_parts) == 1:
return path_parts[0]
root = ""
anchor = ""
path = []
for new_root, tail in map(_os._path_splitroot, path_parts):
if new_root.startswith(path_sep_tuple) or new_root.endswith(path_sep_tuple):
root = new_root.rstrip(path_separators) or root
for drive, root, tail in map(_os._path_splitroot, path_parts):
nineteendo marked this conversation as resolved.
Show resolved Hide resolved
new_anchor = drive + root
if new_anchor.startswith(path_sep_tuple) or new_anchor.endswith(path_sep_tuple):
anchor = new_anchor.rstrip(path_separators) or anchor
path = [path_sep + tail]
elif new_root.endswith(':'):
if root.casefold() != new_root.casefold():
elif new_anchor.endswith(':'):
if anchor.casefold() != new_anchor.casefold():
# Drive relative paths have to be resolved by the OS, so we reset the
# tail but do not add a path_sep prefix.
root = new_root
anchor = new_anchor
path = [tail]
else:
path.append(tail)
else:
root = new_root or root
anchor = new_anchor or anchor
path.append(tail)
path = [p.rstrip(path_separators) for p in path if p]
if len(path) == 1 and not path[0]:
# Avoid losing the root's trailing separator when joining with nothing
return root + path_sep
return root + path_sep.join(path)
# Avoid losing the anchor's trailing separator when joining with nothing
return anchor + path_sep
return anchor + path_sep.join(path)

else:
def _path_join(*path_parts):
Expand Down Expand Up @@ -178,8 +179,9 @@ def _path_isabs(path):
"""Replacement for os.path.isabs."""
if not path:
return False
root = _os._path_splitroot(path)[0].replace('/', '\\')
return len(root) > 1 and (root.startswith('\\\\') or root.endswith('\\'))
drive, root, _ = _os._path_splitroot(path)
anchor = (drive + root).replace('/', '\\')
return len(anchor) > 1 and (anchor.startswith('\\\\') or anchor.endswith('\\'))

else:
def _path_isabs(path):
Expand Down
116 changes: 68 additions & 48 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,56 +167,76 @@ def splitdrive(p):
return drive, root + tail


def splitroot(p):
"""Split a pathname into drive, root and tail. The drive is defined
exactly as in splitdrive(). On Windows, the root may be a single path
separator or an empty string. The tail contains anything after the root.
For example:

splitroot('//server/share/') == ('//server/share', '/', '')
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
"""
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'\\'
altsep = b'/'
colon = b':'
unc_prefix = b'\\\\?\\UNC\\'
empty = b''
else:
sep = '\\'
altsep = '/'
colon = ':'
unc_prefix = '\\\\?\\UNC\\'
empty = ''
normp = p.replace(altsep, sep)
if normp[:1] == sep:
if normp[1:2] == sep:
# UNC drives, e.g. \\server\share or \\?\UNC\server\share
# Device drives, e.g. \\.\device or \\?\device
start = 8 if normp[:8].upper() == unc_prefix else 2
index = normp.find(sep, start)
if index == -1:
return p, empty, empty
index2 = normp.find(sep, index + 1)
if index2 == -1:
return p, empty, empty
return p[:index2], p[index2:index2 + 1], p[index2 + 1:]
try:
from nt import _path_splitroot
except ImportError:
def splitroot(p):
"""Split a pathname into drive, root and tail. The drive is defined
exactly as in splitdrive(). On Windows, the root may be a single path
separator or an empty string. The tail contains anything after the root.
For example:

splitroot('//server/share/') == ('//server/share', '/', '')
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
"""
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'\\'
altsep = b'/'
colon = b':'
unc_prefix = b'\\\\?\\UNC\\'
empty = b''
else:
# Relative path with root, e.g. \Windows
return empty, p[:1], p[1:]
elif normp[1:2] == colon:
if normp[2:3] == sep:
# Absolute drive-letter path, e.g. X:\Windows
return p[:2], p[2:3], p[3:]
sep = '\\'
altsep = '/'
colon = ':'
unc_prefix = '\\\\?\\UNC\\'
empty = ''
normp = p.replace(altsep, sep)
if normp[:1] == sep:
if normp[1:2] == sep:
# UNC drives, e.g. \\server\share or \\?\UNC\server\share
# Device drives, e.g. \\.\device or \\?\device
start = 8 if normp[:8].upper() == unc_prefix else 2
index = normp.find(sep, start)
if index == -1:
return p, empty, empty
index2 = normp.find(sep, index + 1)
if index2 == -1:
return p, empty, empty
return p[:index2], p[index2:index2 + 1], p[index2 + 1:]
else:
# Relative path with root, e.g. \Windows
return empty, p[:1], p[1:]
elif normp[1:2] == colon:
if normp[2:3] == sep:
# Absolute drive-letter path, e.g. X:\Windows
return p[:2], p[2:3], p[3:]
else:
# Relative path with drive, e.g. X:Windows
return p[:2], empty, p[2:]
else:
# Relative path with drive, e.g. X:Windows
return p[:2], empty, p[2:]
else:
# Relative path, e.g. Windows
return empty, empty, p
# Relative path, e.g. Windows
return empty, empty, p
else:
def splitroot(p):
"""Split a pathname into drive, root and tail. The drive is defined
exactly as in splitdrive(). On Windows, the root may be a single path
separator or an empty string. The tail contains anything after the root.
For example:

splitroot('//server/share/') == ('//server/share', '/', '')
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
"""
p = os.fspath(p)
if isinstance(p, bytes):
drive, root, tail = _path_splitroot(os.fsdecode(p))
return os.fsencode(drive), os.fsencode(root), os.fsencode(tail)
return _path_splitroot(p)


# Split a path in head (everything up to the last '/') and tail (the
Expand Down
73 changes: 46 additions & 27 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,52 @@ def splitdrive(p):
return p[:0], p


def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
empty; the root may be empty, a single slash, or two slashes. The tail
contains anything after the root. For example:

splitroot('foo/bar') == ('', '', 'foo/bar')
splitroot('/foo/bar') == ('', '/', 'foo/bar')
splitroot('//foo/bar') == ('', '//', 'foo/bar')
splitroot('///foo/bar') == ('', '/', '//foo/bar')
"""
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'/'
empty = b''
else:
sep = '/'
empty = ''
if p[:1] != sep:
# Relative path, e.g.: 'foo'
return empty, empty, p
elif p[1:2] != sep or p[2:3] == sep:
# Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
return empty, sep, p[1:]
else:
# Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
return empty, p[:2], p[2:]
try:
from posix import _path_splitroot
except ImportError:
def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
empty; the root may be empty, a single slash, or two slashes. The tail
contains anything after the root. For example:

splitroot('foo/bar') == ('', '', 'foo/bar')
splitroot('/foo/bar') == ('', '/', 'foo/bar')
splitroot('//foo/bar') == ('', '//', 'foo/bar')
splitroot('///foo/bar') == ('', '/', '//foo/bar')
"""
p = os.fspath(p)
if isinstance(p, bytes):
sep = b'/'
empty = b''
else:
sep = '/'
empty = ''
if p[:1] != sep:
# Relative path, e.g.: 'foo'
return empty, empty, p
elif p[1:2] != sep or p[2:3] == sep:
# Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
return empty, sep, p[1:]
else:
# Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
return empty, p[:2], p[2:]
else:
def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
empty; the root may be empty, a single slash, or two slashes. The tail
contains anything after the root. For example:

splitroot('foo/bar') == ('', '', 'foo/bar')
splitroot('/foo/bar') == ('', '/', 'foo/bar')
splitroot('//foo/bar') == ('', '//', 'foo/bar')
splitroot('///foo/bar') == ('', '/', '//foo/bar')
"""
p = os.fspath(p)
if isinstance(p, bytes):
_, root, tail = _path_splitroot(os.fsdecode(p))
return b'', os.fsencode(root), os.fsencode(tail)
return _path_splitroot(p)


# Return the tail (basename) part of a path, same as split(path)[1].
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ def test_normpath(self):
tester("ntpath.normpath('\\\\foo\\')", '\\\\foo\\')
tester("ntpath.normpath('\\\\foo')", '\\\\foo')
tester("ntpath.normpath('\\\\')", '\\\\')
tester("ntpath.normpath('//?/UNC/server/share/..')", '\\\\?\\UNC\\server\\share\\')

def test_realpath_curdir(self):
expected = ntpath.normpath(os.getcwd())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speedup :func:`os.path.splitroot`.
nineteendo marked this conversation as resolved.
Show resolved Hide resolved
98 changes: 29 additions & 69 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading