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
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
Allow embedded null
  • Loading branch information
nineteendo committed Apr 23, 2024
commit 30d613ba80af1f6564315bd648751994494f07f0
18 changes: 7 additions & 11 deletions Modules/clinic/posixmodule.c.h

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

33 changes: 23 additions & 10 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ path_cleanup(path_t *path)
}

static int
path_converter(PyObject *o, void *p)
posix_path_converter(PyObject *o, void *p, int allow_embedded_null)
{
path_t *path = (path_t *)p;
PyObject *bytes = NULL;
Expand Down Expand Up @@ -1293,7 +1293,7 @@ path_converter(PyObject *o, void *p)
FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
goto error_exit;
}
if (wcslen(wide) != length) {
if (!allow_embedded_null && wcslen(wide) != length) {
FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
goto error_exit;
}
Expand Down Expand Up @@ -1341,7 +1341,7 @@ path_converter(PyObject *o, void *p)

length = PyBytes_GET_SIZE(bytes);
narrow = PyBytes_AS_STRING(bytes);
if ((size_t)length != strlen(narrow)) {
if (!allow_embedded_null && (size_t)length != strlen(narrow)) {
FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
goto error_exit;
}
Expand All @@ -1364,7 +1364,7 @@ path_converter(PyObject *o, void *p)
FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
goto error_exit;
}
if (wcslen(wide) != length) {
if (!allow_embedded_null && wcslen(wide) != length) {
FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
goto error_exit;
}
Expand Down Expand Up @@ -1400,6 +1400,12 @@ path_converter(PyObject *o, void *p)
return 0;
}

static int
path_converter(PyObject *o, void *p)
{
return posix_path_converter(o, p, 0);
}

static void
argument_unavailable_error(const char *function_name, const char *argument_name)
{
Expand Down Expand Up @@ -5470,31 +5476,38 @@ os__path_islink_impl(PyObject *module, PyObject *path)
/*[clinic input]
os._path_splitroot_ex

p: path_t
p: object

Split a pathname into drive, root and tail.
[clinic start generated code]*/

static PyObject *
os__path_splitroot_ex_impl(PyObject *module, path_t *p)
/*[clinic end generated code: output=2001f8839dda3762 input=3b4aad8eba96cfef]*/
os__path_splitroot_ex_impl(PyObject *module, PyObject *p)
/*[clinic end generated code: output=1be3aff51db9fc0d input=df3394f511f02c51]*/
{
Py_ssize_t len = p->length, drvsize, rootsize;
Py_ssize_t len, drvsize, rootsize;
PyObject *drv = NULL, *root = NULL, *tail = NULL, *result = NULL;
const wchar_t *wide = p->wide;
path_t path = PATH_T_INITIALIZE("_path_splitroot_ex", "p", 0, 0);
if (!posix_path_converter(p, &path, 1)) {
goto exit;
}
len = path.length;
const wchar_t *wide = path.wide;
_Py_skiproot(wide, len, &drvsize, &rootsize);
if (!(drv = PyUnicode_FromWideChar(wide, drvsize)) ||
!(root = PyUnicode_FromWideChar(&wide[drvsize], rootsize)) ||
!(tail = PyUnicode_FromWideChar(&wide[drvsize + rootsize], len - drvsize - rootsize)))
{
goto exit;
}
if (p->narrow) {
if (path.narrow) {
Py_SETREF(drv, PyUnicode_EncodeFSDefault(drv));
Py_SETREF(root, PyUnicode_EncodeFSDefault(root));
Py_SETREF(tail, PyUnicode_EncodeFSDefault(tail));
}
result = Py_BuildValue("(OOO)", drv, root, tail);
exit:
path_cleanup(&path);
Py_DECREF(drv);
Py_DECREF(root);
Py_DECREF(tail);
Expand Down
0