Skip to content

Commit

Permalink
fix #1279: catch and skip ENODEV in net_if_stat()
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jul 15, 2018
1 parent f440b6e commit cf9d1cd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ XXXX-XX-XX
task_for_pid() syscall. AccessDenied is now raised instead.
- 1278_: [macOS] Process.threads() incorrectly return microseconds instead of
seconds. (patch by Nikhil Marathe)
- 1279_: [Linux, macOS, BSD] net_if_stats() may return ENODEV.

5.4.6
=====
Expand Down
8 changes: 4 additions & 4 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1880,11 +1880,11 @@ Process class
(OpenBSD) "laddr" and "raddr" fields for UNIX sockets are always set to
"". This is a limitation of the OS.

.. versionchanged:: 5.3.0 : "laddr" and "raddr" are named tuples.
.. note::
(AIX) :class:`psutil.AccessDenied` is always raised unless running
as root (lsof does the same).

.. note::
(AIX) :class:`psutil.AccessDenied` is always raised unless running
as root (lsof does the same).
.. versionchanged:: 5.3.0 : "laddr" and "raddr" are named tuples.

.. method:: is_running()

Expand Down
18 changes: 12 additions & 6 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,18 @@ def net_if_stats():
names = net_io_counters().keys()
ret = {}
for name in names:
mtu = cext_posix.net_if_mtu(name)
isup = cext_posix.net_if_flags(name)
duplex, speed = cext_posix.net_if_duplex_speed(name)
if hasattr(_common, 'NicDuplex'):
duplex = _common.NicDuplex(duplex)
ret[name] = _common.snicstats(isup, duplex, speed, mtu)
try:
mtu = cext_posix.net_if_mtu(name)
isup = cext_posix.net_if_flags(name)
duplex, speed = cext_posix.net_if_duplex_speed(name)
except OSError as err:
# https://github.com/giampaolo/psutil/issues/1279
if err.errno != errno.ENODEV:
raise
else:
if hasattr(_common, 'NicDuplex'):
duplex = _common.NicDuplex(duplex)
ret[name] = _common.snicstats(isup, duplex, speed, mtu)
return ret


Expand Down
14 changes: 10 additions & 4 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,16 @@ def net_if_stats():
names = net_io_counters().keys()
ret = {}
for name in names:
mtu = cext_posix.net_if_mtu(name)
isup = cext_posix.net_if_flags(name)
duplex, speed = cext.net_if_duplex_speed(name)
ret[name] = _common.snicstats(isup, duplex_map[duplex], speed, mtu)
try:
mtu = cext_posix.net_if_mtu(name)
isup = cext_posix.net_if_flags(name)
duplex, speed = cext.net_if_duplex_speed(name)
except OSError as err:
# https://github.com/giampaolo/psutil/issues/1279
if err.errno != errno.ENODEV:
raise
else:
ret[name] = _common.snicstats(isup, duplex_map[duplex], speed, mtu)
return ret


Expand Down
18 changes: 12 additions & 6 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,18 @@ def net_if_stats():
names = net_io_counters().keys()
ret = {}
for name in names:
mtu = cext_posix.net_if_mtu(name)
isup = cext_posix.net_if_flags(name)
duplex, speed = cext_posix.net_if_duplex_speed(name)
if hasattr(_common, 'NicDuplex'):
duplex = _common.NicDuplex(duplex)
ret[name] = _common.snicstats(isup, duplex, speed, mtu)
try:
mtu = cext_posix.net_if_mtu(name)
isup = cext_posix.net_if_flags(name)
duplex, speed = cext_posix.net_if_duplex_speed(name)
except OSError as err:
# https://github.com/giampaolo/psutil/issues/1279
if err.errno != errno.ENODEV:
raise
else:
if hasattr(_common, 'NicDuplex'):
duplex = _common.NicDuplex(duplex)
ret[name] = _common.snicstats(isup, duplex, speed, mtu)
return ret


Expand Down
10 changes: 10 additions & 0 deletions psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,16 @@ def test_net_if_stats(self):
self.assertGreaterEqual(speed, 0)
self.assertGreaterEqual(mtu, 0)

@unittest.skipIf(not (LINUX or BSD or MACOS),
"LINUX or BSD or MACOS specific")
def test_net_if_stats_enodev(self):
# See: https://github.com/giampaolo/psutil/issues/1279
with mock.patch('psutil._psutil_posix.net_if_mtu',
side_effect=OSError(errno.ENODEV, "")) as m:
ret = psutil.net_if_stats()
self.assertEqual(ret, {})
assert m.called

@unittest.skipIf(LINUX and not os.path.exists('/proc/diskstats'),
'/proc/diskstats not available on this linux version')
@unittest.skipIf(APPVEYOR and psutil.disk_io_counters() is None,
Expand Down

0 comments on commit cf9d1cd

Please sign in to comment.