summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-04-09 17:29:43 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-04-09 17:29:43 +0200
commit3f67acceb089029563c423517b5508bdc9cb6356 (patch)
tree394294dacf3b6f52ac7ed1df268998cbe0a75bd8
parent5994e49648ef98d668fb0517dcca79e5a1a750fd (diff)
downloadpsutil-3f67acceb089029563c423517b5508bdc9cb6356.tar.gz
fix #1009: sensors_temperatures() may raise OSError.
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/_pslinux.py12
-rwxr-xr-xpsutil/tests/test_linux.py19
3 files changed, 30 insertions, 2 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index 2c01ee8d..a5715287 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 ddc3ce50..71eae5bd 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 2e3183ca..730ffece 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -1200,6 +1200,25 @@ class TestSensorsBattery(unittest.TestCase):
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
# =====================================================================