Skip to content

Commit

Permalink
#376: make net_if_addr() work on all other UNIX variants; also skip p…
Browse files Browse the repository at this point in the history
…rimary addresses which cannot be termined
  • Loading branch information
giampaolo committed Jan 2, 2015
1 parent 512579d commit f54979e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def net_connections(kind):
disk_usage = _psposix.disk_usage
net_io_counters = cext.net_io_counters
disk_io_counters = cext.disk_io_counters
net_if_addrs = _psutil_posix.net_if_addrs


def wrap_exceptions(fun):
Expand Down
1 change: 1 addition & 0 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def net_connections(kind='inet'):
disk_usage = _psposix.disk_usage
net_io_counters = cext.net_io_counters
disk_io_counters = cext.disk_io_counters
net_if_addrs = _psutil_posix.net_if_addrs


def wrap_exceptions(fun):
Expand Down
1 change: 1 addition & 0 deletions psutil/_pssunos.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
disk_io_counters = cext.disk_io_counters
net_io_counters = cext.net_io_counters
disk_usage = _psposix.disk_usage
net_if_addrs = _psutil_posix.net_if_addrs


def virtual_memory():
Expand Down
5 changes: 5 additions & 0 deletions psutil/_psutil_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ psutil_net_if_addrs(PyObject* self, PyObject* args)
PyObject *py_netmask = NULL;
PyObject *py_broadcast = NULL;


if (getifaddrs(&ifaddr) == -1) {
PyErr_SetFromErrno(PyExc_OSError);
goto error;
Expand All @@ -171,6 +172,10 @@ psutil_net_if_addrs(PyObject* self, PyObject* args)
continue;
family = ifa->ifa_addr->sa_family;
py_address = psutil_convert_ipaddr(ifa->ifa_addr, family);
// If the primary address can't be determined just skip it.
// I've never seen this happen on Linux but I did on FreeBSD.
if (py_address == Py_None)
continue;
if (py_address == NULL)
goto error;
py_netmask = psutil_convert_ipaddr(ifa->ifa_netmask, family);
Expand Down

0 comments on commit f54979e

Please sign in to comment.