Skip to content

Commit

Permalink
fix #1009: sensors_temperatures() may raise OSError.
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 9, 2017
1 parent 5994e49 commit 3f67acc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- 1004_: [Linux] Process.io_counters() may raise ValueError.
- 1006_: [Linux] cpu_freq() may return None on some Linux versions does not
support the function; now the function is not declared instead.
- 1009_: [Linux] sensors_temperatures() may raise OSError.

*2017-03-24*

Expand Down
12 changes: 10 additions & 2 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,10 +1112,18 @@ def sensors_temperatures():
for base in basenames:
unit_name = cat(os.path.join(os.path.dirname(base), 'name'),
binary=False)
label = cat(base + '_label', fallback='', binary=False)
current = float(cat(base + '_input')) / 1000.0
high = cat(base + '_max', fallback=None)
critical = cat(base + '_crit', fallback=None)
label = cat(base + '_label', fallback='', binary=False)
try:
current = float(cat(base + '_input')) / 1000.0
except OSError as err:
# https://github.com/giampaolo/psutil/issues/1009
if err.errno == errno.EIO:
warnings.warn("ignoring %r" % err, RuntimeWarning)
continue
else:
raise

if high is not None:
high = float(high) / 1000.0
Expand Down
19 changes: 19 additions & 0 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,25 @@ def open_mock(name, *args, **kwargs):
assert m.called


@unittest.skipUnless(LINUX, "LINUX only")
class TestSensorsTemperatures(unittest.TestCase):

def test_emulate_eio_error(self):
def open_mock(name, *args, **kwargs):
if name.endswith("_input"):
raise OSError(errno.EIO, "")
else:
return orig_open(name, *args, **kwargs)

orig_open = open
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
with mock.patch(patch_point, side_effect=open_mock) as m:
with warnings.catch_warnings(record=True) as ws:
self.assertEqual(psutil.sensors_temperatures(), {})
assert m.called
self.assertIn("ignoring", str(ws[0].message))


# =====================================================================
# --- test process
# =====================================================================
Expand Down

0 comments on commit 3f67acc

Please sign in to comment.