Skip to content

Commit d73d077

Browse files
committed
Swap order of tests to avoid unnecessary failed API calls
1 parent 39bec11 commit d73d077

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

Modules/posixmodule.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,30 @@ win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
17301730
}
17311731

17321732
if (hFile != INVALID_HANDLE_VALUE) {
1733+
/* Handle types other than files on disk. */
1734+
fileType = GetFileType(hFile);
1735+
if (fileType != FILE_TYPE_DISK) {
1736+
if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1737+
retval = -1;
1738+
goto cleanup;
1739+
}
1740+
DWORD fileAttributes = GetFileAttributesW(path);
1741+
memset(result, 0, sizeof(*result));
1742+
if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1743+
fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1744+
/* \\.\pipe\ or \\.\mailslot\ */
1745+
result->st_mode = _S_IFDIR;
1746+
} else if (fileType == FILE_TYPE_CHAR) {
1747+
/* \\.\nul */
1748+
result->st_mode = _S_IFCHR;
1749+
} else if (fileType == FILE_TYPE_PIPE) {
1750+
/* \\.\pipe\spam */
1751+
result->st_mode = _S_IFIFO;
1752+
}
1753+
/* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1754+
goto cleanup;
1755+
}
1756+
17331757
/* Query the reparse tag, and traverse a non-link. */
17341758
if (!traverse) {
17351759
if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
@@ -1765,30 +1789,6 @@ win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
17651789
}
17661790
}
17671791

1768-
fileType = GetFileType(hFile);
1769-
if (fileType != FILE_TYPE_DISK) {
1770-
/* Handle file types other than files on disk. */
1771-
if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1772-
retval = -1;
1773-
goto cleanup;
1774-
}
1775-
DWORD fileAttributes = GetFileAttributesW(path);
1776-
memset(result, 0, sizeof(*result));
1777-
if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1778-
fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1779-
/* \\.\pipe\ or \\.\mailslot\ */
1780-
result->st_mode = _S_IFDIR;
1781-
} else if (fileType == FILE_TYPE_CHAR) {
1782-
/* \\.\nul */
1783-
result->st_mode = _S_IFCHR;
1784-
} else if (fileType == FILE_TYPE_PIPE) {
1785-
/* \\.\pipe\spam */
1786-
result->st_mode = _S_IFIFO;
1787-
}
1788-
/* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1789-
goto cleanup;
1790-
}
1791-
17921792
if (!GetFileInformationByHandle(hFile, &fileInfo)) {
17931793
switch (GetLastError()) {
17941794
case ERROR_INVALID_PARAMETER:

0 commit comments

Comments
 (0)