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,6 +558,7 @@ def main_xonsh(args):
if err_type is SystemExit:
raise err
else:
if XSH.env.get("XONSH_SHOW_TRACEBACK"):
traceback.print_exception(*exc_info)
exit_code = 1
events.on_exit.fire()

View file

@ -594,7 +594,10 @@ 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"):
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: