diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-03 14:34:54 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-03 14:34:54 +0200 |
commit | b06bb03ea543e6e53dc5eda601111855c15e3f7d (patch) | |
tree | 26ea65cb3cb3eae65944617e8264fff1dd6743ad | |
parent | 31d6c2eb265c20446efc90ab6c46d4a097f29b5f (diff) | |
download | psutil-b06bb03ea543e6e53dc5eda601111855c15e3f7d.tar.gz |
re #1040 have users() use WTSQuerySessionInformationW in order to return
unicode. Also fixes #1048 (host IP address was invalid).
-rw-r--r-- | HISTORY.rst | 1 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 30 |
2 files changed, 13 insertions, 18 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index 67213b2c..30bdecc3 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -37,6 +37,7 @@ None - 1046_: [Windows] disk_partitions() on Windows overrides user's SetErrorMode. - 1047_: [Windows] Process username(): memory leak in case exception is thrown. +- 1048_: [Windows] users()'s host field report an invalid IP address. *2017-04-10* diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 5758c3e3..2ce98e17 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2576,7 +2576,7 @@ error: static PyObject * psutil_users(PyObject *self, PyObject *args) { HANDLE hServer = WTS_CURRENT_SERVER_HANDLE; - LPTSTR buffer_user = NULL; + WCHAR *buffer_user = NULL; LPTSTR buffer_addr = NULL; PWTS_SESSION_INFO sessions = NULL; DWORD count; @@ -2595,7 +2595,7 @@ psutil_users(PyObject *self, PyObject *args) { PyObject *py_retlist = PyList_New(0); PyObject *py_tuple = NULL; PyObject *py_address = NULL; - PyObject *py_buffer_user_encoded = NULL; + PyObject *py_username = NULL; if (py_retlist == NULL) return NULL; @@ -2623,12 +2623,12 @@ psutil_users(PyObject *self, PyObject *args) { // username bytes = 0; - if (WTSQuerySessionInformation(hServer, sessionId, WTSUserName, - &buffer_user, &bytes) == 0) { + if (WTSQuerySessionInformationW(hServer, sessionId, WTSUserName, + &buffer_user, &bytes) == 0) { PyErr_SetFromWindowsErr(0); goto error; } - if (bytes == 1) + if (bytes <= 2) continue; // address @@ -2672,24 +2672,18 @@ psutil_users(PyObject *self, PyObject *args) { station_info.ConnectTime.dwLowDateTime - 116444736000000000LL; unix_time /= 10000000; -#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 - py_buffer_user_encoded = PyUnicode_DecodeLocaleAndSize( - buffer_user, _tcslen(buffer_user), "surrogateescape"); -#else - py_buffer_user_encoded = PyUnicode_Decode( - buffer_user, _tcslen(buffer_user), Py_FileSystemDefaultEncoding, - "replace"); -#endif - - if (py_buffer_user_encoded == NULL) + py_username = PyUnicode_FromWideChar(buffer_user, wcslen(buffer_user)); + if (py_username == NULL) goto error; - py_tuple = Py_BuildValue("OOd", py_buffer_user_encoded, py_address, + py_tuple = Py_BuildValue("OOd", + py_username, + py_address, (double)unix_time); if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; - Py_XDECREF(py_buffer_user_encoded); + Py_XDECREF(py_username); Py_XDECREF(py_address); Py_XDECREF(py_tuple); } @@ -2701,7 +2695,7 @@ psutil_users(PyObject *self, PyObject *args) { return py_retlist; error: - Py_XDECREF(py_buffer_user_encoded); + Py_XDECREF(py_username); Py_XDECREF(py_tuple); Py_XDECREF(py_address); Py_DECREF(py_retlist); |