From 3f67acceb089029563c423517b5508bdc9cb6356 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 9 Apr 2017 17:29:43 +0200 Subject: [PATCH] fix #1009: sensors_temperatures() may raise OSError. --- HISTORY.rst | 1 + psutil/_pslinux.py | 12 ++++++++++-- psutil/tests/test_linux.py | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 2c01ee8db..a57152872 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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* diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index ddc3ce50c..71eae5bd8 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -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 diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index 2e3183ca3..730ffeced 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -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 # =====================================================================