[Python-checkins] bpo-45582: Add a NOT operator to the condition in getpath_isxfile (GH-29906)

zooba webhook-mailer at python.org
Fri Dec 3 17:04:21 EST 2021


https://github.com/python/cpython/commit/7d7c91a8e8c0bb04105a21a17d1061ffc1c04d80
commit: 7d7c91a8e8c0bb04105a21a17d1061ffc1c04d80
branch: main
author: neonene <53406459+neonene at users.noreply.github.com>
committer: zooba <steve.dower at microsoft.com>
date: 2021-12-03T22:04:11Z
summary:

bpo-45582: Add a NOT operator to the condition in getpath_isxfile (GH-29906)

files:
M Modules/getpath.c

diff --git a/Modules/getpath.c b/Modules/getpath.c
index f66fc8abbd4ff..c8c85a8540d39 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -173,7 +173,9 @@ getpath_isdir(PyObject *Py_UNUSED(self), PyObject *args)
     path = PyUnicode_AsWideCharString(pathobj, NULL);
     if (path) {
 #ifdef MS_WINDOWS
-        r = (GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
+        DWORD attr = GetFileAttributesW(path);
+        r = (attr != INVALID_FILE_ATTRIBUTES) &&
+            (attr & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
 #else
         struct stat st;
         r = (_Py_wstat(path, &st) == 0) && S_ISDIR(st.st_mode) ? Py_True : Py_False;
@@ -197,7 +199,9 @@ getpath_isfile(PyObject *Py_UNUSED(self), PyObject *args)
     path = PyUnicode_AsWideCharString(pathobj, NULL);
     if (path) {
 #ifdef MS_WINDOWS
-        r = !(GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
+        DWORD attr = GetFileAttributesW(path);
+        r = (attr != INVALID_FILE_ATTRIBUTES) &&
+            !(attr & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
 #else
         struct stat st;
         r = (_Py_wstat(path, &st) == 0) && S_ISREG(st.st_mode) ? Py_True : Py_False;
@@ -223,7 +227,9 @@ getpath_isxfile(PyObject *Py_UNUSED(self), PyObject *args)
     if (path) {
 #ifdef MS_WINDOWS
         const wchar_t *ext;
-        r = (GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) &&
+        DWORD attr = GetFileAttributesW(path);
+        r = (attr != INVALID_FILE_ATTRIBUTES) &&
+            !(attr & FILE_ATTRIBUTE_DIRECTORY) &&
             SUCCEEDED(PathCchFindExtension(path, cchPath, &ext)) &&
             (CompareStringOrdinal(ext, -1, L".exe", -1, 1 /* ignore case */) == CSTR_EQUAL)
             ? Py_True : Py_False;



More information about the Python-checkins mailing list