diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-02 04:57:08 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-02 04:57:08 +0200 |
commit | 753faf8488796ad5ed838c901788ea0fc8a4f57e (patch) | |
tree | e45f9b8c7b30b846207162e5c8d1462f4f7f8106 | |
parent | 79e30c23053a2eb9ecdea6a78cbd43e4cb2dfe91 (diff) | |
download | psutil-753faf8488796ad5ed838c901788ea0fc8a4f57e.tar.gz |
#1040 users() / linux: fix unicode
-rw-r--r-- | psutil/_psutil_linux.c | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index 5c090712..1a96fea0 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -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) @@ -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 @@ -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; |