]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
authorBrian Curtin <brian@python.org>
Wed, 8 Jun 2011 23:17:18 +0000 (18:17 -0500)
committerBrian Curtin <brian@python.org>
Wed, 8 Jun 2011 23:17:18 +0000 (18:17 -0500)
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.

Lib/ntpath.py
Misc/NEWS
Modules/posixmodule.c

index ec8a7ab23750a3b51d27d7d0cea1c9d746a41aa2..2483ced7fff5019fbf5e33d2a1d8bc741e8cd4bc 100644 (file)
@@ -672,3 +672,16 @@ except ImportError:
 def sameopenfile(f1, f2):
     """Test whether two file objects reference the same file"""
     return _getfileinformation(f1) == _getfileinformation(f2)
+
+
+try:
+    # The genericpath.isdir implementation uses os.stat and checks the mode
+    # attribute to tell whether or not the path is a directory.
+    # This is overkill on Windows - just pass the path to GetFileAttributes
+    # and check the attribute from there.
+    from nt import _isdir
+except ImportError:
+    from genericpath import isdir as _isdir
+
+def isdir(path):
+    return _isdir(path)
index 16a0f290fb9e6dc84e3bbcb95d0f70ef2ed38b39..15ed46f8e0bdf19c97c4a6b85b6059a9fe87ba99 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #11583: Speed up os.path.isdir on Windows by using GetFileAttributes
+  instead of os.stat.
+
 - Named tuples now work correctly with vars().
 
 - Issue #12085: Fix an attribute error in subprocess.Popen destructor if the
index 89d3f2f2d0b783d32eb2a9307692f9d6ce5bfe29..767ed612903af84fe9e1a0c247403354a2a060ff 100644 (file)
@@ -2819,6 +2819,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__,
@@ -8055,6 +8091,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__},