Skip to content

Commit

Permalink
fix #565: proper encoding for username() and users() on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 31, 2014
1 parent c0bfb28 commit e93f2cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,8 @@ I: 496

N: spacewander
E: spacewanderlzx@gmail.com
I: 561
I: 561

N: Sylvain Mouquet
E: sylvain.mouquet@gmail.com
I: 565
10 changes: 6 additions & 4 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ Bug tracker at https://github.com/giampaolo/psutil/issues

**Bug fixes**

- #496: [Solaris]: can't import psutil.
- #547: [UNIX]: Process.username() may raise KeyError if UID can't be resolved.
- #556: [Linux]: lots of file handles were left open.
- #561: [Linux]: net_connections() might skip some legitimate UNIX sockets.
- #496: [Solaris] can't import psutil.
- #547: [UNIX] Process.username() may raise KeyError if UID can't be resolved.
- #556: [Linux] lots of file handles were left open.
- #561: [Linux] net_connections() might skip some legitimate UNIX sockets.
(patch by spacewander)
- #565: [Windows] use proper encoding for psutil.Process.username() and
psutil.users(). (patch by Sylvain Mouquet)


2.1.3 - 2014-09-26
Expand Down
14 changes: 11 additions & 3 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,8 @@ psutil_proc_username(PyObject *self, PyObject *args)
memcpy(&fullName[domainNameSize + 1], name, nameSize);
fullName[domainNameSize + 1 + nameSize] = '\0';

returnObject = Py_BuildValue("s", fullName);
returnObject = PyUnicode_Decode(
fullName, _tcslen(fullName), Py_FileSystemDefaultEncoding, "replace");

free(fullName);
free(name);
Expand Down Expand Up @@ -2655,6 +2656,8 @@ 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;

if (py_retlist == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2739,12 +2742,16 @@ psutil_users(PyObject *self, PyObject *args)
station_info.ConnectTime.dwLowDateTime - 116444736000000000LL;
unix_time /= 10000000;

py_tuple = Py_BuildValue("sOd", buffer_user, py_address,
py_buffer_user_encoded = PyUnicode_Decode(
buffer_user, _tcslen(buffer_user), Py_FileSystemDefaultEncoding,
"replace");
py_tuple = Py_BuildValue("OOd", py_buffer_user_encoded, 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_address);
Py_XDECREF(py_tuple);
}
Expand All @@ -2757,6 +2764,7 @@ psutil_users(PyObject *self, PyObject *args)
return py_retlist;

error:
Py_XDECREF(py_buffer_user_encoded);
Py_XDECREF(py_tuple);
Py_XDECREF(py_address);
Py_DECREF(py_retlist);
Expand Down Expand Up @@ -3232,4 +3240,4 @@ void init_psutil_windows(void)
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
}

0 comments on commit e93f2cc

Please sign in to comment.