Skip to content

Commit

Permalink
fix: PyArg_ParseTuple "u" has been removed in python 3.12
Browse files Browse the repository at this point in the history
From https://docs.python.org/3/c-api/arg.html

Deprecated since version 3.3, will be removed in version 3.12: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsWideCharString().

Signed-off-by: mayeut <[email protected]>
  • Loading branch information
mayeut committed Jul 29, 2023
1 parent 5c73716 commit 8c0e3df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ XXXX-XX-XX
instead of OverflowError. (patch by Xuehai Pan)
- 2268_: ``bytes2human()`` utility function was unable to properly represent
negative values.
- 2252_: [Windows]: `psutil.disk_usage`_ fails on Python 3.12+. (patch by Matthieu Darbois)

5.9.5
=====
Expand Down
29 changes: 25 additions & 4 deletions psutil/arch/windows/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ PyObject *
psutil_disk_usage(PyObject *self, PyObject *args) {
BOOL retval;
ULARGE_INTEGER _, total, free;

#if PY_MAJOR_VERSION <= 2
char *path;

if (PyArg_ParseTuple(args, "u", &path)) {
Expand All @@ -55,23 +57,42 @@ psutil_disk_usage(PyObject *self, PyObject *args) {

// on Python 2 we also want to accept plain strings other
// than Unicode
#if PY_MAJOR_VERSION <= 2
PyErr_Clear(); // drop the argument parsing error
if (PyArg_ParseTuple(args, "s", &path)) {
Py_BEGIN_ALLOW_THREADS
retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
Py_END_ALLOW_THREADS
goto return_;
}
#endif

return NULL;

return_:
if (retval == 0)
return PyErr_SetFromWindowsErrWithFilename(0, path);
else
return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
#else
PyObject *py_path;
wchar_t *path;

if (!PyArg_ParseTuple(args, "U", &py_path)) {
return NULL;
}

path = PyUnicode_AsWideCharString(py_path, NULL);
if (path == NULL) {
return NULL;
}

Py_BEGIN_ALLOW_THREADS
retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Py_END_ALLOW_THREADS

PyMem_Free(path);

if (retval == 0)
return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, py_path);
#endif
return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
}


Expand Down

0 comments on commit 8c0e3df

Please sign in to comment.