From d41e6ddf72377ecfc6b175370882e8561544a2c2 Mon Sep 17 00:00:00 2001 From: Daniel Shimon Date: Wed, 5 Aug 2020 17:42:19 +0300 Subject: [PATCH] Call repr() when pretty() fails in _pprint_displayhook --- news/print-mocks-correctly.rst | 23 +++++++++++++++++++++++ xonsh/main.py | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 news/print-mocks-correctly.rst diff --git a/news/print-mocks-correctly.rst b/news/print-mocks-correctly.rst new file mode 100644 index 0000000000..2e0943a0fa --- /dev/null +++ b/news/print-mocks-correctly.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Viewing mock objects in the shell + +**Security:** + +* diff --git a/xonsh/main.py b/xonsh/main.py index 0a3c71d29d..ce3ac3b3c9 100644 --- a/xonsh/main.py +++ b/xonsh/main.py @@ -251,9 +251,11 @@ def _pprint_displayhook(value): builtins._ = value return env = builtins.__xonsh__.env + printed_val = None if env.get("PRETTY_PRINT_RESULTS"): printed_val = pretty(value) - else: + if not isinstance(printed_val, str): + # pretty may fail (i.e for unittest.mock.Mock) printed_val = repr(value) if HAS_PYGMENTS and env.get("COLOR_RESULTS"): tokens = list(pygments.lex(printed_val, lexer=pyghooks.XonshLexer()))