diff options
author | Brian Curtin <brian@python.org> | 2011-06-08 23:43:57 (GMT) |
---|---|---|
committer | Brian Curtin <brian@python.org> | 2011-06-08 23:43:57 (GMT) |
commit | 9a82eaade3cbfba9895b6121daf8ddb65b5aaa4d (patch) | |
tree | 8257efd366978aee3774e35858f00e62b6b1a089 /Modules | |
parent | 4dd453c6aab315954f19bf0b2f1541c2b37d8e34 (diff) | |
parent | 9c669ccc77c85eac245d460bab510a38b20d9a08 (diff) | |
download | cpython-9a82eaade3cbfba9895b6121daf8ddb65b5aaa4d.zip cpython-9a82eaade3cbfba9895b6121daf8ddb65b5aaa4d.tar.gz cpython-9a82eaade3cbfba9895b6121daf8ddb65b5aaa4d.tar.bz2 |
Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
By changing to the Windows GetFileAttributes API in nt._isdir we can figure
out if the path is a directory without opening the file via os.stat. This has
the minor benefit of speeding up os.path.isdir by at least 2x for regular
files and 10-15x improvements were seen on symbolic links (which opened the
file multiple times during os.stat). Since os.path.isdir is used in
several places on interpreter startup, we get a minor speedup in startup time.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index acc420f..f9b3a43 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2960,6 +2960,42 @@ posix__getfileinformation(PyObject *self, PyObject *args) info.nFileIndexHigh, info.nFileIndexLow); } + +static PyObject * +posix__isdir(PyObject *self, PyObject *args) +{ + PyObject *opath; + char *path; + PyUnicodeObject *po; + DWORD attributes; + + if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { + Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); + + attributes = GetFileAttributesW(wpath); + if (attributes == INVALID_FILE_ATTRIBUTES) + Py_RETURN_FALSE; + goto check; + } + /* Drop the argument parsing error as narrow strings + are also valid. */ + PyErr_Clear(); + + if (!PyArg_ParseTuple(args, "O&:_isdir", + PyUnicode_FSConverter, &opath)) + return NULL; + + path = PyBytes_AsString(opath); + attributes = GetFileAttributesA(path); + if (attributes == INVALID_FILE_ATTRIBUTES) + Py_RETURN_FALSE; + +check: + if (attributes & FILE_ATTRIBUTE_DIRECTORY) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} #endif /* MS_WINDOWS */ PyDoc_STRVAR(posix_mkdir__doc__, @@ -9561,6 +9597,7 @@ static PyMethodDef posix_methods[] = { {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, + {"_isdir", posix__isdir, METH_VARARGS, NULL}, #endif #ifdef HAVE_GETLOADAVG {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__}, |