diff options
author | Joe Pamer <jopamer@users.noreply.github.com> | 2018-09-25 14:57:36 (GMT) |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-09-25 14:57:36 (GMT) |
commit | c8c0249c9e8f61ab7670119a5a5278354df27bbb (patch) | |
tree | 79ccb9272364f0e7265b4cedce939b4df5129d38 /Modules/posixmodule.c | |
parent | 604e7b9931f9e7881a2941816e538f5f15930db8 (diff) | |
download | cpython-c8c0249c9e8f61ab7670119a5a5278354df27bbb.zip cpython-c8c0249c9e8f61ab7670119a5a5278354df27bbb.tar.gz cpython-c8c0249c9e8f61ab7670119a5a5278354df27bbb.tar.bz2 |
bpo-32557: allow shutil.disk_usage to take a file path on Windows also (GH-9372)
https://bugs.python.org/issue32557
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 400ed97..c7223ab 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -10079,13 +10079,38 @@ os__getdiskusage_impl(PyObject *module, path_t *path) { BOOL retval; ULARGE_INTEGER _, total, free; + DWORD err = 0; Py_BEGIN_ALLOW_THREADS retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); Py_END_ALLOW_THREADS - if (retval == 0) - return PyErr_SetFromWindowsErr(0); + if (retval == 0) { + if (GetLastError() == ERROR_DIRECTORY) { + wchar_t *dir_path = NULL; + + dir_path = PyMem_New(wchar_t, path->length + 1); + if (dir_path == NULL) { + return PyErr_NoMemory(); + } + + wcscpy_s(dir_path, path->length + 1, path->wide); + + if (_dirnameW(dir_path) != -1) { + Py_BEGIN_ALLOW_THREADS + retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); + Py_END_ALLOW_THREADS + } + /* Record the last error in case it's modified by PyMem_Free. */ + err = GetLastError(); + PyMem_Free(dir_path); + if (retval) { + goto success; + } + } + return PyErr_SetFromWindowsErr(err); + } +success: return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); } #endif /* MS_WINDOWS */ |