8000 gh-102511: Add C implementation of `os.path.splitroot()` by nineteendo · Pull Request #118089 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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 29 commits into from
Apr 25, 2024
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
8000 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
Prev Previous commit
Next Next commit
Rename to _path_splitroot replacing old one
  • Loading branch information
nineteendo committed Apr 20, 2024
commit 54911528b71b1dbfc7141c91eae42a1cc398e05c
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):
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
6 changes: 3 additions & 3 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def splitdrive(p):


try:
from nt import _path_splitroot_ex
from nt import _path_splitroot
except ImportError:
def splitroot(p):
"""Split a pathname into drive, root and tail. The drive is defined
Expand Down Expand Up @@ -234,9 +234,9 @@ def splitroot(p):
"""
p = os.fspath(p)
if isinstance(p, bytes):
drive, root, tail = _path_splitroot_ex(os.fsdecode(p))
drive, root, tail = _path_splitroot(os.fsdecode(p))
return os.fsencode(drive), os.fsencode(root), os.fsencode(tail)
return _path_splitroot_ex(p)
return _path_splitroot(p)


# Split a path in head (everything up to the last '/') and tail (the
Expand Down
6 changes: 3 additions & 3 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def splitdrive(p):


try:
from posix import _path_splitroot_ex
from posix import _path_splitroot
except ImportError:
def splitroot(p):
"""Split a pathname into drive, root and tail. On Posix, drive is always
Expand Down Expand Up @@ -177,9 +177,9 @@ def splitroot(p):
"""
p = os.fspath(p)
if isinstance(p, bytes):
_, root, tail = _path_splitroot_ex(os.fsdecode(p))
_, root, tail = _path_splitroot(os.fsdecode(p))
return b'', os.fsencode(root), os.fsencode(tail)
return _path_splitroot_ex(p)
return _path_splitroot(p)


# Return the tail (basename) part of a path, same as split(path)[1].
Expand Down
86 changes: 9 additions & 77 deletions Modules/clinic/posixmodule.c.h

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

53 changes: 3 additions & 50 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5040,52 +5040,6 @@ os__getvolumepathname_impl(PyObject *module, path_t *path)
}


/*[clinic input]
os._path_splitroot

path: path_t

Removes everything after the root on Win32.
[clinic start generated code]*/

static PyObject *
os__path_splitroot_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=ab7f1a88b654581c input=dc93b1d3984cffb6]*/
{
wchar_t *buffer;
wchar_t *end;
PyObject *result = NULL;
HRESULT ret;

buffer = (wchar_t*)PyMem_Malloc(sizeof(wchar_t) * (wcslen(path->wide) + 1));
if (!buffer) {
return NULL;
}
wcscpy(buffer, path->wide);
for (wchar_t *p = wcschr(buffer, L'/'); p; p = wcschr(p, L'/')) {
*p = L'\\';
}

Py_BEGIN_ALLOW_THREADS
ret = PathCchSkipRoot(buffer, &end);
Py_END_ALLOW_THREADS
if (FAILED(ret)) {
result = Py_BuildValue("sO", "", path->object);
} else if (end != buffer) {
size_t rootLen = (size_t)(end - buffer);
result = Py_BuildValue("NN",
PyUnicode_FromWideChar(path->wide, rootLen),
PyUnicode_FromWideChar(path->wide + rootLen, -1)
);
} else {
result = Py_BuildValue("Os", path->object, "");
}
PyMem_Free(buffer);

return result;
}


/*[clinic input]
os._path_isdir

Expand Down Expand Up @@ -5468,16 +5422,16 @@ os__path_islink_impl(PyObject *module, PyObject *path)


/*[clinic input]
os._path_splitroot_ex
os._path_splitroot

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]*/
os__path_splitroot_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=6904e00a6a970b9b input=4ef301247820b583]*/
{
Py_ssize_t len, drvsize, rootsize;
wchar_t *buffer = PyUnicode_AsWideCharString(path, &len);
Expand Down Expand Up @@ -16826,7 +16780,6 @@ 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
0