Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add C implementation of ntpath.splitroot()
  • Loading branch information
nineteendo committed Apr 19, 2024
commit c397a23922d7d2e7beabee4d834689cfebe1b2b8
4 changes: 4 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ 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) */

#ifdef MS_WINDOWS
extern void _Py_skiproot(wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize);
#endif

// Macros to protect CRT calls against instant termination when passed an
// invalid parameter (bpo-23524). IPH stands for Invalid Parameter Handler.
// Usage:
Expand Down
117 changes: 68 additions & 49 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,57 +166,76 @@ def splitdrive(p):
drive, root, tail = splitroot(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_ex
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_ex(os.fsdecode(p))
return os.fsencode(drive), os.fsencode(root), os.fsencode(tail)
return _path_splitroot_ex(p)


# Split a path in head (everything up to the last '/') and tail (the
Expand Down
38 changes: 37 additions & 1 deletion Modules/clinic/posixmodule.c.h

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

29 changes: 29 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5085,6 +5085,34 @@ os__path_splitroot_impl(PyObject *module, path_t *path)
return result;
}

#ifdef MS_WINDOWS
/*[clinic input]
os._path_splitroot_ex

path: unicode
/

[clinic start generated code]*/

static PyObject *
os__path_splitroot_ex_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=de97403d3dfebc40 input=bebce42edb41f967]*/
{
Py_ssize_t len;
wchar_t *buffer = PyUnicode_AsWideCharString(path, &len);
Py_ssize_t drvsize;
Py_ssize_t rootsize;
_Py_skiproot(buffer, len, &drvsize, &rootsize);
wchar_t *p = buffer;
PyObject *drv = PyUnicode_FromWideChar(p, drvsize);
p += drvsize;
PyObject *root = PyUnicode_FromWideChar(p, rootsize);
p += rootsize;
PyObject *tail = PyUnicode_FromWideChar(p, len - drvsize - rootsize);
PyMem_Free(buffer);
return Py_BuildValue("(OOO)", drv, root, tail);
}
#endif

/*[clinic input]
os._path_isdir
Expand Down Expand Up @@ -16799,6 +16827,7 @@ static PyMethodDef posix_methods[] = {
OS__FINDFIRSTFILE_METHODDEF
OS__GETVOLUMEPATHNAME_METHODDEF
OS__PATH_SPLITROOT_METHODDEF
OS__PATH_SPLITROOT_EX_METHODDEF
OS__PATH_NORMPATH_METHODDEF
OS_GETLOADAVG_METHODDEF
OS_URANDOM_METHODDEF
Expand Down
57 changes: 57 additions & 0 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,63 @@ PathCchCombineEx(wchar_t *buffer, size_t bufsize, const wchar_t *dirname,

#endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */

#ifdef MS_WINDOWS
void
_Py_skiproot(wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize)
Comment thread
nineteendo marked this conversation as resolved.
Outdated
Comment thread
nineteendo marked this conversation as resolved.
Outdated
{
Comment thread
nineteendo marked this conversation as resolved.
wchar_t *pEnd = size >= 0 ? &path[size] : NULL;
#define IS_END(x) (pEnd ? (x) == pEnd : !*(x))
#define IS_SEP(x) (*(x) == SEP || *(x) == ALTSEP)
*drvsize = 0;
*rootsize = 0;
if (IS_SEP(&path[0])) {
if (IS_SEP(&path[1])) {
// Device drives, e.g. \\.\device or \\?\device
// UNC drives, e.g. \\server\share or \\?\UNC\server\share
Py_ssize_t idx;
if (path[2] == L'?' && IS_SEP(&path[3]) && (path[4] == L'U' || path[4] == L'u') &&
(path[5] == L'N' || path[5] == L'n') && (path[6] == L'C' || path[6] == L'c') &&
IS_SEP(&path[7])) {
idx = 8;
}
else {
idx = 2;
}
while (!IS_END(&path[idx]) && !IS_SEP(&path[idx])) {
idx++;
}
if (IS_END(&path[idx])) {
*drvsize = idx;
}
else {
idx++;
while (!IS_END(&path[idx]) && !IS_SEP(&path[idx])) {
idx++;
}
*drvsize = idx;
if (IS_SEP(&path[idx])) {
*rootsize = 1;
}
}
}
else {
// Relative path with root, e.g. \Windows
*rootsize = 1;
}
}
else if (!IS_END(&path[0]) && path[1] == L':')
{
*drvsize = 2;
if (IS_SEP(&path[2])) {
// Absolute drive-letter path, e.g. X:\Windows
*rootsize = 1;
}
}
#undef IS_SEP
#undef IS_END
}
#endif

// The caller must ensure "buffer" is big enough.
static int
join_relfile(wchar_t *buffer, size_t bufsize,
Expand Down