diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-24 19:38:52 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-24 19:38:52 +0100 |
commit | e49a9919ec043b476833feb592286b2c14533e82 (patch) | |
tree | 218dd6fd09347eb4a005690b4d9a02cadf6bdb04 | |
parent | a39d66047f3e3f9b65c212e3320c5268246d0b1a (diff) | |
download | psutil-e49a9919ec043b476833feb592286b2c14533e82.tar.gz |
declare PID converter functions
-rw-r--r-- | psutil/_psutil_common.c | 35 | ||||
-rw-r--r-- | psutil/_psutil_common.h | 3 | ||||
-rw-r--r-- | psutil/_psutil_linux.c | 26 |
3 files changed, 55 insertions, 9 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index 9075fda3..e433f797 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -64,6 +64,23 @@ PyErr_SetFromOSErrnoWithSyscall(const char *syscall) { } +#if PY_MAJOR_VERSION == 2 +PyObject * +PyLong_FromPid(pid_t pid) { + if ((sizeof(pid_t) == sizeof(int)) || (sizeof(pid_t) == sizeof(long))) { + return PyInt_FromLong(pid); + } + else if (sizeof(pid_t) == sizeof(long long)) { + return PyLong_FromLongLong(pid); + } + else { + PyErr_SetString(PyExc_ValueError, "can't get size of pid_t"); + return NULL; + } +} +#endif + + // ==================================================================== // --- Custom exceptions // ==================================================================== @@ -148,6 +165,24 @@ psutil_setup(void) { } +int +Py_PidConverter(PyObject *arg, void *addr) { + if ((sizeof(pid_t) == sizeof(int)) || (sizeof(pid_t) == sizeof(long))) { + *((pid_t *)addr) = PyLong_AsLong(arg); + } + else if (sizeof(pid_t) == sizeof(long long)) { + *((pid_t *)addr) = PyLong_AsLongLong(arg); + } + else { + PyErr_SetString(PyExc_ValueError, "can't get size of pid_t"); + } + + if (PyErr_Occurred()) + return 0; + return 1; +} + + // ==================================================================== // --- Windows // ==================================================================== diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index d47f25d9..a99b6337 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -32,6 +32,8 @@ static const int PSUTIL_CONN_NONE = 128; #else #define _Py_PARSE_PID "l" #endif + + PyObject *PyLong_FromPid(pid_t pid); #endif // ==================================================================== @@ -49,6 +51,7 @@ PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall); PyObject* psutil_set_testing(PyObject *self, PyObject *args); void psutil_debug(const char* format, ...); int psutil_setup(void); +int Py_PidConverter(PyObject *arg, void *addr); // ==================================================================== // --- Windows diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index 3d9ead83..17835f1a 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -99,7 +99,7 @@ static PyObject * psutil_proc_ioprio_get(PyObject *self, PyObject *args) { pid_t pid; int ioprio, ioclass, iodata; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid); if (ioprio == -1) @@ -121,8 +121,10 @@ psutil_proc_ioprio_set(PyObject *self, PyObject *args) { int ioprio, ioclass, iodata; int retval; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "ii", &pid, &ioclass, &iodata)) + if (! PyArg_ParseTuple( + args, "O&ii", Py_PidConverter, &pid, &ioclass, &iodata)) { return NULL; + } ioprio = IOPRIO_PRIO_VALUE(ioclass, iodata); retval = ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio); if (retval == -1) @@ -147,10 +149,10 @@ psutil_linux_prlimit(PyObject *self, PyObject *args) { PyObject *py_soft = NULL; PyObject *py_hard = NULL; - if (! PyArg_ParseTuple( - args, _Py_PARSE_PID "i|OO", &pid, &resource, &py_soft, &py_hard)) { + if (! PyArg_ParseTuple(args, "O&i|OO", Py_PidConverter, &pid, &resource, + &py_soft, &py_hard)) { return NULL; -} + } // get if (py_soft == NULL && py_hard == NULL) { @@ -297,7 +299,7 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) { cpu_set_t *mask = NULL; PyObject *py_list = NULL; - if (!PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (!PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; ncpus = NCPUS_START; while (1) { @@ -365,7 +367,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { PyObject *py_cpu_set; PyObject *py_cpu_seq = NULL; - if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O", &pid, &py_cpu_set)) + if (!PyArg_ParseTuple(args, "O&O", Py_PidConverter, &pid, &py_cpu_set)) return NULL; if (!PySequence_Check(py_cpu_set)) { @@ -423,6 +425,7 @@ psutil_users(PyObject *self, PyObject *args) { PyObject *py_tty = NULL; PyObject *py_hostname = NULL; PyObject *py_user_proc = NULL; + PyObject *py_pid = NULL; if (py_retlist == NULL) return NULL; @@ -443,14 +446,17 @@ psutil_users(PyObject *self, PyObject *args) { py_hostname = PyUnicode_DecodeFSDefault(ut->ut_host); if (! py_hostname) goto error; + py_pid = PyLong_FromPid(ut->ut_pid); + if (! py_pid) + goto error; py_tuple = Py_BuildValue( - "(OOOfO" _Py_PARSE_PID ")", + "(OOOfOO)", py_username, // username py_tty, // tty py_hostname, // hostname (float)ut->ut_tv.tv_sec, // tstamp py_user_proc, // (bool) user process - ut->ut_pid // process id + py_pid // process id ); if (! py_tuple) goto error; @@ -460,6 +466,7 @@ psutil_users(PyObject *self, PyObject *args) { Py_CLEAR(py_tty); Py_CLEAR(py_hostname); Py_CLEAR(py_tuple); + Py_CLEAR(py_pid); } endutent(); return py_retlist; @@ -469,6 +476,7 @@ error: Py_XDECREF(py_tty); Py_XDECREF(py_hostname); Py_XDECREF(py_tuple); + Py_XDECREF(py_pid); Py_DECREF(py_retlist); endutent(); return NULL; |