From 5bd44f8afcecbfb0db479ce230c790fc2c56569a Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 13 Aug 2018 14:12:04 +0200 Subject: [PATCH] fix #1323: [Linux] sensors_temperatures() may fail with ValueError --- HISTORY.rst | 1 + psutil/_pslinux.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e9162de5b..7532112df 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -30,6 +30,7 @@ XXXX-XX-XX statuses (returns '?'). - 1313_: [Linux] disk_io_counters() can report inflated IO counters due to erroneously counting base disk device and its partition(s) twice. +- 1323_: [Linux] sensors_temperatures() may fail with ValueError. 5.4.6 ===== diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 63b4ded1e..df624de32 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -318,7 +318,7 @@ def cat(fname, fallback=_DEFAULT, binary=True): try: with open_binary(fname) if binary else open_text(fname) as f: return f.read().strip() - except IOError: + except (IOError, OSError): if fallback is not _DEFAULT: return fallback else: @@ -1199,13 +1199,15 @@ def sensors_temperatures(): current = float(cat(path)) / 1000.0 path = os.path.join(os.path.dirname(base), 'name') unit_name = cat(path, binary=False) - except (IOError, OSError) as err: + except (IOError, OSError, ValueError) as err: # A lot of things can go wrong here, so let's just skip the - # whole entry. + # whole entry. Sure thing is Linux's /sys/class/hwmon really + # is a stinky broken mess. # https://github.com/giampaolo/psutil/issues/1009 # https://github.com/giampaolo/psutil/issues/1101 # https://github.com/giampaolo/psutil/issues/1129 # https://github.com/giampaolo/psutil/issues/1245 + # https://github.com/giampaolo/psutil/issues/1323 warnings.warn("ignoring %r for file %r" % (err, path), RuntimeWarning) continue @@ -1215,9 +1217,15 @@ def sensors_temperatures(): label = cat(base + '_label', fallback='', binary=False) if high is not None: - high = float(high) / 1000.0 + try: + high = float(high) / 1000.0 + except ValueError: + high = None if critical is not None: - critical = float(critical) / 1000.0 + try: + critical = float(critical) / 1000.0 + except ValueError: + critical = None ret[unit_name].append((label, current, high, critical))