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 <anki-code@users.noreply.github.com>
This commit is contained in:
Ivan Ogasawara 2023-02-27 15:07:45 -04:00 committed by GitHub
parent b600c58766
commit 0c713a0c31
Failed to generate hash of commit
3 changed files with 34 additions and 7 deletions

23
news/pr-5066.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Suppress subprocess traceback on exception in case ``$XONSH_SHOW_TRACEBACK=False`` with ``$RAISE_SUBPROC_ERROR=True``.
**Security:**
* <news item>

View file

@ -558,7 +558,8 @@ def main_xonsh(args):
if err_type is SystemExit: if err_type is SystemExit:
raise err raise err
else: else:
traceback.print_exception(*exc_info) if XSH.env.get("XONSH_SHOW_TRACEBACK"):
traceback.print_exception(*exc_info)
exit_code = 1 exit_code = 1
events.on_exit.fire() events.on_exit.fire()
postmain(args) postmain(args)

View file

@ -594,12 +594,15 @@ class CommandPipeline:
"""Raises a subprocess error, if we are supposed to.""" """Raises a subprocess error, if we are supposed to."""
spec = self.spec spec = self.spec
rtn = self.returncode rtn = self.returncode
if rtn is not None and rtn != 0 and XSH.env.get("RAISE_SUBPROC_ERROR"):
try: if rtn is None or rtn == 0 or not XSH.env.get("RAISE_SUBPROC_ERROR"):
raise subprocess.CalledProcessError(rtn, spec.args, output=self.output) return
finally:
# this is need to get a working terminal in interactive mode try:
self._return_terminal() 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 # Properties