gh-98393: os module reject bytes-like, only accept bytes (#98394)

The os module and the PyUnicode_FSDecoder() function no longer accept
bytes-like paths, like bytearray and memoryview types: only the exact
bytes type is accepted for bytes strings.
This commit is contained in:
Victor Stinner 2022-10-18 17:52:31 +02:00 committed by GitHub
parent 9da5215000
commit db03c8066a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 48 additions and 89 deletions

View file

@ -1120,7 +1120,7 @@ path_converter(PyObject *o, void *p)
path_t *path = (path_t *)p;
PyObject *bytes = NULL;
Py_ssize_t length = 0;
int is_index, is_buffer, is_bytes, is_unicode;
int is_index, is_bytes, is_unicode;
const char *narrow;
#ifdef MS_WINDOWS
PyObject *wo = NULL;
@ -1158,11 +1158,10 @@ path_converter(PyObject *o, void *p)
/* Only call this here so that we don't treat the return value of
os.fspath() as an fd or buffer. */
is_index = path->allow_fd && PyIndex_Check(o);
is_buffer = PyObject_CheckBuffer(o);
is_bytes = PyBytes_Check(o);
is_unicode = PyUnicode_Check(o);
if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
if (!is_index && !is_unicode && !is_bytes) {
/* Inline PyOS_FSPath() for better error messages. */
PyObject *func, *res;
@ -1225,27 +1224,6 @@ path_converter(PyObject *o, void *p)
bytes = o;
Py_INCREF(bytes);
}
else if (is_buffer) {
/* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
after removing support of non-bytes buffer objects. */
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"%s%s%s should be %s, not %.200s",
path->function_name ? path->function_name : "",
path->function_name ? ": " : "",
path->argument_name ? path->argument_name : "path",
path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
"integer or None" :
path->allow_fd ? "string, bytes, os.PathLike or integer" :
path->nullable ? "string, bytes, os.PathLike or None" :
"string, bytes or os.PathLike",
_PyType_Name(Py_TYPE(o)))) {
goto error_exit;
}
bytes = PyBytes_FromObject(o);
if (!bytes) {
goto error_exit;
}
}
else if (is_index) {
if (!_fd_converter(o, &path->fd)) {
goto error_exit;
@ -4126,8 +4104,8 @@ _posix_listdir(path_t *path, PyObject *list)
const char *name;
if (path->narrow) {
name = path->narrow;
/* only return bytes if they specified a bytes-like object */
return_str = !PyObject_CheckBuffer(path->object);
/* only return bytes if they specified a bytes object */
return_str = !PyBytes_Check(path->object);
}
else {
name = ".";
@ -14049,7 +14027,7 @@ DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
goto error;
}
if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
if (!path->narrow || !PyBytes_Check(path->object)) {
entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
if (joined_path)
entry->path = PyUnicode_DecodeFSDefault(joined_path);