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
Prev Previous commit
Next Next commit
Rename old function to _path_splitanchor
Co-authored-by: Steve Dower <steve.dower@microsoft.com>
  • Loading branch information
nineteendo and zooba committed Apr 22, 2024
commit ca0761a50d90c0eb524554556b69bf1424b0763f
28 changes: 13 additions & 15 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,28 @@ def _path_join(*path_parts):
return ""
if len(path_parts) == 1:
return path_parts[0]
anchor = ""
root = ""
path = []
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
for new_root, tail in map(_os._path_splitanchor, path_parts):
if new_root.startswith(path_sep_tuple) or new_root.endswith(path_sep_tuple):
root = new_root.rstrip(path_separators) or root
path = [path_sep + tail]
elif new_anchor.endswith(':'):
if anchor.casefold() != new_anchor.casefold():
elif new_root.endswith(':'):
if root.casefold() != new_root.casefold():
# Drive relative paths have to be resolved by the OS, so we reset the
# tail but do not add a path_sep prefix.
anchor = new_anchor
root = new_root
path = [tail]
else:
path.append(tail)
else:
anchor = new_anchor or anchor
root = new_root or root
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 anchor's trailing separator when joining with nothing
return anchor + path_sep
return anchor + path_sep.join(path)
# Avoid losing the root's trailing separator when joining with nothing
return root + path_sep
return root + path_sep.join(path)

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

else:
def _path_isabs(path):
Expand Down
70 changes: 69 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.

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


/*[clinic input]
os._path_splitanchor
path: path_t
Removes everything after the root on Win32.
[clinic start generated code]*/

static PyObject *
os__path_splitanchor_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=37b687463b40c424 input=00dd0c06233b8cff]*/
{
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 @@ -5434,13 +5478,21 @@ os__path_splitroot_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=6904e00a6a970b9b input=4ef301247820b583]*/
{
Py_ssize_t len, drvsize, rootsize;
PyObject *drv, *root, *tail, *result = NULL;
wchar_t *buffer = PyUnicode_AsWideCharString(path, &len);
if (!buffer) {
goto exit;
}
_Py_skiproot(buffer, len, &drvsize, &rootsize);
PyObject *drv = PyUnicode_FromWideChar(buffer, drvsize);
PyObject *root = PyUnicode_FromWideChar(&buffer[drvsize], rootsize);
PyObject *tail = PyUnicode_FromWideChar(&buffer[drvsize + rootsize], len - drvsize - rootsize);
if (!(drv = PyUnicode_FromWideChar(buffer, drvsize)) ||
!(root = PyUnicode_FromWideChar(&buffer[drvsize], rootsize)) ||
!(tail = PyUnicode_FromWideChar(&buffer[drvsize + rootsize], len - drvsize - rootsize)))
{
goto exit;
}
Comment thread
nineteendo marked this conversation as resolved.
Outdated
result = Py_BuildValue("(OOO)", drv, root, tail);
exit:
PyMem_Free(buffer);
PyObject *result = Py_BuildValue("(OOO)", drv, root, tail);
Py_DECREF(drv);
Py_DECREF(root);
Py_DECREF(tail);
Expand Down Expand Up @@ -16779,6 +16831,7 @@ static PyMethodDef posix_methods[] = {
OS__GETFINALPATHNAME_METHODDEF
OS__FINDFIRSTFILE_METHODDEF
OS__GETVOLUMEPATHNAME_METHODDEF
OS__PATH_SPLITANCHOR_METHODDEF
OS__PATH_SPLITROOT_METHODDEF
OS__PATH_NORMPATH_METHODDEF
OS_GETLOADAVG_METHODDEF
Expand Down
4 changes: 3 additions & 1 deletion Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2296,8 +2296,10 @@ PathCchCombineEx(wchar_t *buffer, size_t bufsize, const wchar_t *dirname,
#endif /* defined(MS_WINDOWS_GAMES) && !defined(MS_WINDOWS_DESKTOP) */

void
_Py_skiproot(wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize)
_Py_skiproot(const wchar_t *path, Py_ssize_t size, Py_ssize_t *drvsize, Py_ssize_t *rootsize)
{
Comment thread
nineteendo marked this conversation as resolved.
assert(drvsize);
assert(rootsize);
#ifndef MS_WINDOWS
#define IS_SEP(x) (*(x) == SEP)
*drvsize = 0;
Expand Down