mirror of
https://github.com/python/cpython
synced 2026-09-29 08:05:30 +03:00
bpo-33721: Make some os.path functions and pathlib.Path methods be tolerant to invalid paths. (#7695)
Such functions as os.path.exists(), os.path.lexists(), os.path.isdir(), os.path.isfile(), os.path.islink(), and os.path.ismount() now return False instead of raising ValueError or its subclasses UnicodeEncodeError and UnicodeDecodeError for paths that contain characters or bytes unrepresentative at the OS level.
This commit is contained in:
parent
7bdf28265a
commit
0185f34ddc
15 changed files with 181 additions and 54 deletions
23
Modules/clinic/posixmodule.c.h
generated
23
Modules/clinic/posixmodule.c.h
generated
|
|
@ -1005,27 +1005,6 @@ PyDoc_STRVAR(os__isdir__doc__,
|
|||
#define OS__ISDIR_METHODDEF \
|
||||
{"_isdir", (PyCFunction)os__isdir, METH_O, os__isdir__doc__},
|
||||
|
||||
static PyObject *
|
||||
os__isdir_impl(PyObject *module, path_t *path);
|
||||
|
||||
static PyObject *
|
||||
os__isdir(PyObject *module, PyObject *arg)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
|
||||
|
||||
if (!PyArg_Parse(arg, "O&:_isdir", path_converter, &path)) {
|
||||
goto exit;
|
||||
}
|
||||
return_value = os__isdir_impl(module, &path);
|
||||
|
||||
exit:
|
||||
/* Cleanup for path */
|
||||
path_cleanup(&path);
|
||||
|
||||
return return_value;
|
||||
}
|
||||
|
||||
#endif /* defined(MS_WINDOWS) */
|
||||
|
||||
#if defined(MS_WINDOWS)
|
||||
|
|
@ -6778,4 +6757,4 @@ exit:
|
|||
#ifndef OS_GETRANDOM_METHODDEF
|
||||
#define OS_GETRANDOM_METHODDEF
|
||||
#endif /* !defined(OS_GETRANDOM_METHODDEF) */
|
||||
/*[clinic end generated code: output=0f23518dd4482e66 input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=40cac0135f846202 input=a9049054013a1b77]*/
|
||||
|
|
|
|||
|
|
@ -3821,22 +3821,32 @@ cleanup:
|
|||
/*[clinic input]
|
||||
os._isdir
|
||||
|
||||
path: path_t
|
||||
path as arg: object
|
||||
/
|
||||
|
||||
Return true if the pathname refers to an existing directory.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
os__isdir_impl(PyObject *module, path_t *path)
|
||||
/*[clinic end generated code: output=75f56f32720836cb input=5e0800149c0ad95f]*/
|
||||
os__isdir(PyObject *module, PyObject *arg)
|
||||
/*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/
|
||||
{
|
||||
DWORD attributes;
|
||||
path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
|
||||
|
||||
if (!path_converter(arg, &path)) {
|
||||
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
|
||||
PyErr_Clear();
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
attributes = GetFileAttributesW(path->wide);
|
||||
attributes = GetFileAttributesW(path.wide);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
path_cleanup(&path);
|
||||
if (attributes == INVALID_FILE_ATTRIBUTES)
|
||||
Py_RETURN_FALSE;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue