Skip to content

Commit 7d7c91a

Browse files
authored
bpo-45582: Add a NOT operator to the condition in getpath_isxfile (GH-29906)
1 parent 5bb7ef2 commit 7d7c91a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Modules/getpath.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ getpath_isdir(PyObject *Py_UNUSED(self), PyObject *args)
173173
path = PyUnicode_AsWideCharString(pathobj, NULL);
174174
if (path) {
175175
#ifdef MS_WINDOWS
176-
r = (GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
176+
DWORD attr = GetFileAttributesW(path);
177+
r = (attr != INVALID_FILE_ATTRIBUTES) &&
178+
(attr & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
177179
#else
178180
struct stat st;
179181
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)
197199
path = PyUnicode_AsWideCharString(pathobj, NULL);
198200
if (path) {
199201
#ifdef MS_WINDOWS
200-
r = !(GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
202+
DWORD attr = GetFileAttributesW(path);
203+
r = (attr != INVALID_FILE_ATTRIBUTES) &&
204+
!(attr & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
201205
#else
202206
struct stat st;
203207
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)
223227
if (path) {
224228
#ifdef MS_WINDOWS
225229
const wchar_t *ext;
226-
r = (GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) &&
230+
DWORD attr = GetFileAttributesW(path);
231+
r = (attr != INVALID_FILE_ATTRIBUTES) &&
232+
!(attr & FILE_ATTRIBUTE_DIRECTORY) &&
227233
SUCCEEDED(PathCchFindExtension(path, cchPath, &ext)) &&
228234
(CompareStringOrdinal(ext, -1, L".exe", -1, 1 /* ignore case */) == CSTR_EQUAL)
229235
? Py_True : Py_False;

0 commit comments

Comments
 (0)