summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2022-09-29-15-19-29.gh-issue-94526.wq5m6T.rst4
-rw-r--r--Modules/getpath.c23
2 files changed, 18 insertions, 9 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-29-15-19-29.gh-issue-94526.wq5m6T.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-29-15-19-29.gh-issue-94526.wq5m6T.rst
new file mode 100644
index 0000000..59e389a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-29-15-19-29.gh-issue-94526.wq5m6T.rst
@@ -0,0 +1,4 @@
+Fix the Python path configuration used to initialized :data:`sys.path` at
+Python startup. Paths are no longer encoded to UTF-8/strict to avoid encoding
+errors if it contains surrogate characters (bytes paths are decoded with the
+surrogateescape error handler). Patch by Victor Stinner.
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 9447988..be704ad 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -82,27 +82,32 @@ getpath_abspath(PyObject *Py_UNUSED(self), PyObject *args)
static PyObject *
getpath_basename(PyObject *Py_UNUSED(self), PyObject *args)
{
- const char *path;
- if (!PyArg_ParseTuple(args, "s", &path)) {
+ PyObject *path;
+ if (!PyArg_ParseTuple(args, "U", &path)) {
return NULL;
}
- const char *name = strrchr(path, SEP);
- return PyUnicode_FromString(name ? name + 1 : path);
+ Py_ssize_t end = PyUnicode_GET_LENGTH(path);
+ Py_ssize_t pos = PyUnicode_FindChar(path, SEP, 0, end, -1);
+ if (pos < 0) {
+ return Py_NewRef(path);
+ }
+ return PyUnicode_Substring(path, pos + 1, end);
}
static PyObject *
getpath_dirname(PyObject *Py_UNUSED(self), PyObject *args)
{
- const char *path;
- if (!PyArg_ParseTuple(args, "s", &path)) {
+ PyObject *path;
+ if (!PyArg_ParseTuple(args, "U", &path)) {
return NULL;
}
- const char *name = strrchr(path, SEP);
- if (!name) {
+ Py_ssize_t end = PyUnicode_GET_LENGTH(path);
+ Py_ssize_t pos = PyUnicode_FindChar(path, SEP, 0, end, -1);
+ if (pos < 0) {
return PyUnicode_FromStringAndSize(NULL, 0);
}
- return PyUnicode_FromStringAndSize(path, (name - path));
+ return PyUnicode_Substring(path, 0, pos);
}