From c1b851d172baa8267c071a07b030bb1e5a6ce746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Wed, 21 Mar 2018 18:48:53 +0100 Subject: [PATCH 1/2] Fix UnicodeEncodeError with non ASCII paths on Windows --- psutil/_psutil_windows.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index f164168af..5a14b3c71 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -1278,31 +1278,31 @@ psutil_proc_open_files(PyObject *self, PyObject *args) { /* - Accept a filename's drive in native format like "\Device\HarddiskVolume1\" + Accept a filename's drive in native format like "\Device\HarddiskVolume1\" and return the corresponding drive letter (e.g. "C:\\"). If no match is found return an empty string. */ static PyObject * psutil_win32_QueryDosDevice(PyObject *self, PyObject *args) { - LPCTSTR lpDevicePath; + LPCWSTR lpDevicePath; TCHAR d = TEXT('A'); TCHAR szBuff[5]; - if (!PyArg_ParseTuple(args, "s", &lpDevicePath)) + if (!PyArg_ParseTuple(args, "u", &lpDevicePath)) return NULL; while (d <= TEXT('Z')) { TCHAR szDeviceName[3] = {d, TEXT(':'), TEXT('\0')}; - TCHAR szTarget[512] = {0}; - if (QueryDosDevice(szDeviceName, szTarget, 511) != 0) { - if (_tcscmp(lpDevicePath, szTarget) == 0) { + WCHAR szTarget[512] = {0}; + if (QueryDosDeviceW(szDeviceName, szTarget, 511) != 0) { + if (wcscmp(lpDevicePath, szTarget) == 0) { _stprintf_s(szBuff, _countof(szBuff), TEXT("%c:"), d); - return Py_BuildValue("s", szBuff); + return Py_BuildValue("u", szBuff); } } d++; } - return Py_BuildValue("s", ""); + return Py_BuildValue("u", ""); } From d5ea04df0044d836de9915c23e1326a81a667cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Thu, 22 Mar 2018 12:06:52 +0100 Subject: [PATCH 2/2] Try 2 --- psutil/_psutil_windows.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 5a14b3c71..8c133bcf9 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -1284,25 +1284,29 @@ psutil_proc_open_files(PyObject *self, PyObject *args) { */ static PyObject * psutil_win32_QueryDosDevice(PyObject *self, PyObject *args) { - LPCWSTR lpDevicePath; + LPCTSTR lpDevicePath; TCHAR d = TEXT('A'); TCHAR szBuff[5]; - if (!PyArg_ParseTuple(args, "u", &lpDevicePath)) +#if PY_MAJOR_VERSION <= 2 + if (!PyArg_ParseTuple(args, "O", &lpDevicePath)) +#else + if (!PyArg_ParseTuple(args, "s", &lpDevicePath)) +#endif return NULL; while (d <= TEXT('Z')) { TCHAR szDeviceName[3] = {d, TEXT(':'), TEXT('\0')}; - WCHAR szTarget[512] = {0}; - if (QueryDosDeviceW(szDeviceName, szTarget, 511) != 0) { - if (wcscmp(lpDevicePath, szTarget) == 0) { + TCHAR szTarget[512] = {0}; + if (QueryDosDevice(szDeviceName, szTarget, 511) != 0) { + if (_tcscmp(lpDevicePath, szTarget) == 0) { _stprintf_s(szBuff, _countof(szBuff), TEXT("%c:"), d); - return Py_BuildValue("u", szBuff); + return Py_BuildValue("s", szBuff); } } d++; } - return Py_BuildValue("u", ""); + return Py_BuildValue("s", ""); }