diff options
author | Steve Dower <steve.dower@python.org> | 2019-09-04 21:42:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-04 21:42:54 (GMT) |
commit | 772ec0fad57412daa53d16d7019b6b2fe6e94942 (patch) | |
tree | 29643e09afda8b8f31bc85db1d9b58b0427aeab4 /Modules/posixmodule.c | |
parent | 60bd1f88f21073965a444c8b39c4202d015da5d6 (diff) | |
download | cpython-772ec0fad57412daa53d16d7019b6b2fe6e94942.zip cpython-772ec0fad57412daa53d16d7019b6b2fe6e94942.tar.gz cpython-772ec0fad57412daa53d16d7019b6b2fe6e94942.tar.bz2 |
bpo-38030: Fix os.stat failures on block devices on Windows (GH-15681)
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index c412d07..81704ee 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1794,13 +1794,13 @@ win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, case ERROR_INVALID_PARAMETER: case ERROR_INVALID_FUNCTION: case ERROR_NOT_SUPPORTED: - retval = -1; + /* Volumes and physical disks are block devices, e.g. + \\.\C: and \\.\PhysicalDrive0. */ + memset(result, 0, sizeof(*result)); + result->st_mode = 0x6000; /* S_IFBLK */ goto cleanup; } - /* Volumes and physical disks are block devices, e.g. - \\.\C: and \\.\PhysicalDrive0. */ - memset(result, 0, sizeof(*result)); - result->st_mode = 0x6000; /* S_IFBLK */ + retval = -1; goto cleanup; } } @@ -1827,7 +1827,14 @@ win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, cleanup: if (hFile != INVALID_HANDLE_VALUE) { - CloseHandle(hFile); + /* Preserve last error if we are failing */ + error = retval ? GetLastError() : 0; + if (!CloseHandle(hFile)) { + retval = -1; + } else if (retval) { + /* Restore last error */ + SetLastError(error); + } } return retval; |