Skip to content

Commit

Permalink
#1040 / users() / osx: fix unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed May 2, 2017
1 parent 8c8b510 commit bb60602
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
{
return NULL;
}

return PyUnicode_DecodeFSDefault(pathinfo.pvi_cdir.vip_path);
}

Expand Down Expand Up @@ -1677,19 +1677,31 @@ psutil_disk_io_counters(PyObject *self, PyObject *args) {
static PyObject *
psutil_users(PyObject *self, PyObject *args) {
struct utmpx *utx;
PyObject *py_retlist = PyList_New(0);
PyObject *py_username = NULL;
PyObject *py_tty = NULL;
PyObject *py_hostname = NULL;
PyObject *py_tuple = NULL;
PyObject *py_retlist = PyList_New(0);

if (py_retlist == NULL)
return NULL;
while ((utx = getutxent()) != NULL) {
if (utx->ut_type != USER_PROCESS)
continue;
py_username = PyUnicode_DecodeFSDefault(utx->ut_user);
if (! py_username)
goto error;
py_tty = PyUnicode_DecodeFSDefault(utx->ut_line);
if (! py_tty)
goto error;
py_hostname = PyUnicode_DecodeFSDefault(utx->ut_host);
if (! py_hostname)
goto error;
py_tuple = Py_BuildValue(
"(sssfi)",
utx->ut_user, // username
utx->ut_line, // tty
utx->ut_host, // hostname
"(OOOfi)",
py_username, // username
py_tty, // tty
py_hostname, // hostname
(float)utx->ut_tv.tv_sec, // start time
utx->ut_pid // process id
);
Expand All @@ -1701,13 +1713,19 @@ psutil_users(PyObject *self, PyObject *args) {
endutxent();
goto error;
}
Py_DECREF(py_username);
Py_DECREF(py_tty);
Py_DECREF(py_hostname);
Py_DECREF(py_tuple);
}

endutxent();
return py_retlist;

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

0 comments on commit bb60602

Please sign in to comment.