diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2021-12-21 13:56:53 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2021-12-21 13:56:53 +0100 |
commit | 828f3c3cabd921c000bef1f6b1fa5ba33ae39438 (patch) | |
tree | 5329433bbe8094400bbad92d6a460f4142b05897 | |
parent | 70b5e765cad9e118b12b332e07e05877a4c0e4b3 (diff) | |
download | psutil-828f3c3cabd921c000bef1f6b1fa5ba33ae39438.tar.gz |
move flags C code in its own separate fun
Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
-rw-r--r-- | psutil/_psutil_posix.c | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index 09984a78..4aacf6ee 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -291,7 +291,7 @@ psutil_convert_ipaddr(struct sockaddr *addr, int family) { int -_append_flag(PyObject *py_flags, char *str) { +_psutil_append_flag(PyObject *py_flags, char *str) { PyObject *py_str; py_str = Py_BuildValue("s", str); @@ -304,6 +304,46 @@ _append_flag(PyObject *py_flags, char *str) { } +PyObject* +_psutil_convert_iff_flags(int flags) { + PyObject *py_retlist = PyList_New(0); + + if (py_retlist == NULL) + return NULL; + + if (flags & IFF_UP) { + if (_psutil_append_flag(py_retlist, "up") != 0) + return NULL; + } + if (flags & IFF_BROADCAST) { + if (_psutil_append_flag(py_retlist, "broadcast") != 0) + return NULL; + } + if (flags & IFF_DEBUG) { + if (_psutil_append_flag(py_retlist, "debug") != 0) + return NULL; + } + if (flags & IFF_LOOPBACK) { + if (_psutil_append_flag(py_retlist, "loopback") != 0) + return NULL; + } + if (flags & IFF_POINTOPOINT) { + if (_psutil_append_flag(py_retlist, "pointopoint") != 0) + return NULL; + } + if (flags & IFF_RUNNING) { + if (_psutil_append_flag(py_retlist, "running") != 0) + return NULL; + } + if (flags & IFF_NOARP) { + if (_psutil_append_flag(py_retlist, "noarp") != 0) + return NULL; + } + + return py_retlist; +} + + /* * Return NICs information a-la ifconfig as a list of tuples. * TODO: on Solaris we won't get any MAC address. @@ -314,12 +354,12 @@ psutil_net_if_addrs(PyObject* self, PyObject* args) { int family; PyObject *py_retlist = PyList_New(0); - PyObject *py_flags = NULL; PyObject *py_tuple = NULL; PyObject *py_address = NULL; PyObject *py_netmask = NULL; PyObject *py_broadcast = NULL; PyObject *py_ptp = NULL; + PyObject *py_flags = NULL; if (py_retlist == NULL) return NULL; @@ -363,40 +403,10 @@ psutil_net_if_addrs(PyObject* self, PyObject* args) { if ((py_broadcast == NULL) || (py_ptp == NULL)) goto error; - // handle flags - py_flags = PyList_New(0); + py_flags = _psutil_convert_iff_flags(ifa->ifa_flags); if (py_flags == NULL) goto error; - if (ifa->ifa_flags & IFF_UP) { - if (_append_flag(py_flags, "up") != 0) - goto error; - } - if (ifa->ifa_flags & IFF_BROADCAST) { - if (_append_flag(py_flags, "broadcast") != 0) - goto error; - } - if (ifa->ifa_flags & IFF_DEBUG) { - if (_append_flag(py_flags, "debug") != 0) - goto error; - } - if (ifa->ifa_flags & IFF_LOOPBACK) { - if (_append_flag(py_flags, "loopback") != 0) - goto error; - } - if (ifa->ifa_flags & IFF_POINTOPOINT) { - if (_append_flag(py_flags, "pointopoint") != 0) - goto error; - } - if (ifa->ifa_flags & IFF_RUNNING) { - if (_append_flag(py_flags, "running") != 0) - goto error; - } - if (ifa->ifa_flags & IFF_NOARP) { - if (_append_flag(py_flags, "noarp") != 0) - goto error; - } - py_tuple = Py_BuildValue( "(siOOOOO)", ifa->ifa_name, |