diff options
-rw-r--r-- | HISTORY.rst | 10 | ||||
-rw-r--r-- | psutil/_common.py | 29 |
2 files changed, 36 insertions, 3 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index f439e48e..b200cb3d 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,8 +3,18 @@ 5.9.5 (IN DEVELOPMENT) ====================== +**Enhancements** + +- 2196_: in case of exception, display a cleaner error traceback by hiding the + `KeyError` bit deriving from a missed cache hit. + +**Bug fixes** + - 2164_, [Linux]: compilation fails on kernels < 2.6.27 (e.g. CentOS 5). - 2186_, [FreeBSD]: compilation fails with Clang 15. (patch by Po-Chuan Hsieh) +- 2191_, [Linux]: `disk_partitions()`_: do not unnecessarily read + /proc/filesystems and raise `AccessDenied`_ unless user specified `all=False` + argument. 5.9.4 ===== diff --git a/psutil/_common.py b/psutil/_common.py index 387d9594..da8a96e5 100644 --- a/psutil/_common.py +++ b/psutil/_common.py @@ -366,6 +366,20 @@ class TimeoutExpired(Error): # =================================================================== +# This should be in _compat.py rather than here, but does not work well +# with setup.py importing this module via a sys.path trick. +if PY3: + __builtins__["exec"]("""def raise_from(value, from_value): + try: + raise value from from_value + finally: + value = None + """) +else: + def raise_from(value, from_value): + raise value + + def usage_percent(used, total, round_=None): """Calculate percentage usage of 'used' against 'total'.""" try: @@ -407,7 +421,10 @@ def memoize(fun): try: return cache[key] except KeyError: - ret = cache[key] = fun(*args, **kwargs) + try: + ret = cache[key] = fun(*args, **kwargs) + except Exception as err: + raise raise_from(err, None) return ret def cache_clear(): @@ -452,11 +469,17 @@ def memoize_when_activated(fun): ret = self._cache[fun] except AttributeError: # case 2: we never entered oneshot() ctx - return fun(self) + try: + return fun(self) + except Exception as err: + raise raise_from(err, None) except KeyError: # case 3: we entered oneshot() ctx but there's no cache # for this entry yet - ret = fun(self) + try: + ret = fun(self) + except Exception as err: + raise raise_from(err, None) try: self._cache[fun] = ret except AttributeError: |