Skip to content

Commit

Permalink
#1040 users() / linux: fix unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed May 2, 2017
1 parent 79e30c2 commit 753faf8
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ psutil_users(PyObject *self, PyObject *args) {
struct utmp *ut;
PyObject *py_retlist = PyList_New(0);
PyObject *py_tuple = NULL;
PyObject *py_username = NULL;
PyObject *py_tty = NULL;
PyObject *py_hostname = NULL;
PyObject *py_user_proc = NULL;

if (py_retlist == NULL)
Expand All @@ -463,11 +466,20 @@ psutil_users(PyObject *self, PyObject *args) {
py_user_proc = Py_True;
else
py_user_proc = Py_False;
py_username = PyUnicode_DecodeFSDefault(ut->ut_user);
if (! py_username)
goto error;
py_tty = PyUnicode_DecodeFSDefault(ut->ut_line);
if (! py_tty)
goto error;
py_hostname = PyUnicode_DecodeFSDefault(ut->ut_host);
if (! py_hostname)
goto error;
py_tuple = Py_BuildValue(
"(sssfOi)",
ut->ut_user, // username
ut->ut_line, // tty
ut->ut_host, // hostname
"(OOOfOi)",
py_username, // username
py_tty, // tty
py_username, // hostname
(float)ut->ut_tv.tv_sec, // tstamp
py_user_proc, // (bool) user process
ut->ut_pid // process id
Expand All @@ -476,14 +488,19 @@ psutil_users(PyObject *self, PyObject *args) {
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_DECREF(py_username);
Py_DECREF(py_tty);
Py_DECREF(py_hostname);
Py_DECREF(py_tuple);
}
endutent();
return py_retlist;

error:
Py_XDECREF(py_username);
Py_XDECREF(py_tty);
Py_XDECREF(py_hostname);
Py_XDECREF(py_tuple);
Py_XDECREF(py_user_proc);
Py_DECREF(py_retlist);
endutent();
return NULL;
Expand Down

0 comments on commit 753faf8

Please sign in to comment.