diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2021-10-14 22:52:32 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2021-10-14 22:52:32 +0200 |
commit | e72438f08ac7aa93b33fddcb11298ece28d0150d (patch) | |
tree | 1a8b33a122c85f6deeaf11178ab138078c0a3bf5 | |
parent | e91bae62e6e6ee9c3ad27c1fe3013f929309e1b0 (diff) | |
download | psutil-e72438f08ac7aa93b33fddcb11298ece28d0150d.tar.gz |
Changes to debug() function:
* use str() if exception derives from OSError / EnvironmentError. This
way we will print the file name (if it exists).
* use repr() for any other exception
* add tests for debug() function
* backport contextlib.redirect_stderr
Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
-rw-r--r-- | psutil/_common.py | 6 | ||||
-rw-r--r-- | psutil/_compat.py | 17 | ||||
-rwxr-xr-x | psutil/tests/test_misc.py | 31 |
3 files changed, 53 insertions, 1 deletions
diff --git a/psutil/_common.py b/psutil/_common.py index a2b8bab6..6a2f38e1 100644 --- a/psutil/_common.py +++ b/psutil/_common.py @@ -837,7 +837,11 @@ if bool(os.getenv('PSUTIL_DEBUG', 0)): fname, lineno, func_name, lines, index = inspect.getframeinfo( inspect.currentframe().f_back) if isinstance(msg, Exception): - msg = "ignoring %s" % msg + if isinstance(msg, (OSError, IOError, EnvironmentError)): + # ...because str(exc) may contain info about the file name + msg = "ignoring %s" % msg + else: + msg = "ignoring %r" % msg print("psutil-debug [%s:%s]> %s" % (fname, lineno, msg), # NOQA file=sys.stderr) else: diff --git a/psutil/_compat.py b/psutil/_compat.py index 90938687..e5275f5f 100644 --- a/psutil/_compat.py +++ b/psutil/_compat.py @@ -8,6 +8,7 @@ Python 3 way of doing things). """ import collections +import contextlib import errno import functools import os @@ -25,6 +26,8 @@ __all__ = [ "lru_cache", # shutil module "which", "get_terminal_size", + # contextlib module + "redirect_stderr", # python 3 exceptions "FileNotFoundError", "PermissionError", "ProcessLookupError", "InterruptedError", "ChildProcessError", "FileExistsError"] @@ -430,3 +433,17 @@ try: except ImportError: class SubprocessTimeoutExpired: pass + + +# python 3.5 +try: + from contextlib import redirect_stderr +except ImportError: + @contextlib.contextmanager + def redirect_stderr(new_target): + original = getattr(sys, "stderr") + try: + setattr(sys, "stderr", new_target) + yield new_target + finally: + setattr(sys, "stderr", original) diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index fc9f5b13..70f6a37e 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -22,11 +22,13 @@ import sys from psutil import LINUX from psutil import POSIX from psutil import WINDOWS +from psutil._common import debug from psutil._common import memoize from psutil._common import memoize_when_activated from psutil._common import supports_ipv6 from psutil._common import wrap_numbers from psutil._compat import PY3 +from psutil._compat import redirect_stderr from psutil.tests import APPVEYOR from psutil.tests import CI_TESTING from psutil.tests import HAS_BATTERY @@ -401,6 +403,35 @@ class TestMisc(PsutilTestCase): reload_module(psutil) self.assertIn("version conflict", str(cm.exception).lower()) + def test_debug(self): + if PY3: + from io import StringIO + else: + from StringIO import StringIO + + with redirect_stderr(StringIO()) as f: + debug("hello") + msg = f.getvalue() + assert msg.startswith("psutil-debug"), msg + self.assertIn("hello", msg) + self.assertIn(__file__, msg) + + # supposed to use repr(exc) + with redirect_stderr(StringIO()) as f: + debug(ValueError("this is an error")) + msg = f.getvalue() + self.assertIn("ignoring ValueError", msg) + self.assertIn("'this is an error'", msg) + + # supposed to use str(exc), because of extra info about file name + with redirect_stderr(StringIO()) as f: + exc = OSError(2, "no such file") + exc.filename = "/foo" + debug(exc) + msg = f.getvalue() + self.assertIn("no such file", msg) + self.assertIn("/foo", msg) + # =================================================================== # --- Tests for wrap_numbers() function. |