From 0c713a0c3122a12e5622730ea7c89f81e58537eb Mon Sep 17 00:00:00 2001 From: Ivan Ogasawara Date: Mon, 27 Feb 2023 15:07:45 -0400 Subject: [PATCH] fix: Suppress subprocess traceback in case XONSH_SHOW_TRACEBACK=False and $RAISE_SUBPROC_ERROR=True (#5066) * fix: Add and extra condition for print traceback from subprocess * add news * Apply pre-commit hooks * Fix initial implementation using sys.exit * Update pr-5066.rst * Remove sys.exit --------- Co-authored-by: Andy Kipp --- news/pr-5066.rst | 23 +++++++++++++++++++++++ xonsh/main.py | 3 ++- xonsh/procs/pipelines.py | 15 +++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 news/pr-5066.rst diff --git a/news/pr-5066.rst b/news/pr-5066.rst new file mode 100644 index 000000000..135d2fd3c --- /dev/null +++ b/news/pr-5066.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Suppress subprocess traceback on exception in case ``$XONSH_SHOW_TRACEBACK=False`` with ``$RAISE_SUBPROC_ERROR=True``. + +**Security:** + +* diff --git a/xonsh/main.py b/xonsh/main.py index 838b1fe91..89d111a30 100644 --- a/xonsh/main.py +++ b/xonsh/main.py @@ -558,7 +558,8 @@ def main_xonsh(args): if err_type is SystemExit: raise err else: - traceback.print_exception(*exc_info) + if XSH.env.get("XONSH_SHOW_TRACEBACK"): + traceback.print_exception(*exc_info) exit_code = 1 events.on_exit.fire() postmain(args) diff --git a/xonsh/procs/pipelines.py b/xonsh/procs/pipelines.py index 623b3a8f7..16128e58b 100644 --- a/xonsh/procs/pipelines.py +++ b/xonsh/procs/pipelines.py @@ -594,12 +594,15 @@ class CommandPipeline: """Raises a subprocess error, if we are supposed to.""" spec = self.spec rtn = self.returncode - if rtn is not None and rtn != 0 and XSH.env.get("RAISE_SUBPROC_ERROR"): - try: - raise subprocess.CalledProcessError(rtn, spec.args, output=self.output) - finally: - # this is need to get a working terminal in interactive mode - self._return_terminal() + + if rtn is None or rtn == 0 or not XSH.env.get("RAISE_SUBPROC_ERROR"): + return + + try: + raise subprocess.CalledProcessError(rtn, spec.args, output=self.output) + finally: + # this is need to get a working terminal in interactive mode + self._return_terminal() # # Properties