Catch SystemExit in ProcProxy (#5698)

* catch SystemExit in ProcProxy

* add test

* add news

* update test

* Update fix-procproxy-sys-exit.rst

---------

Co-authored-by: Andy Kipp <anki-code@users.noreply.github.com>
This commit is contained in:
Peter Ye 2024-10-03 12:34:09 -04:00 committed by GitHub
parent 4fc7d59c95
commit 79b3561c21
Failed to generate hash of commit
3 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* Now SystemExit that invoked from callable alias follows to exiting from callable alias instead of exiting the entire shell.
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Built-in commands such as `xonfig -h` and `xontrib -h` no longer cause the shell to exit.
**Security:**
* <news item>

View file

@ -329,6 +329,22 @@ f
"hello\n", "hello\n",
0, 0,
), ),
# test system exit in unthreadable alias (see #5689)
(
"""
from xonsh.tools import unthreadable
@unthreadable
def _f():
import sys
sys.exit(42)
aliases['f'] = _f
print(![f].returncode)
""",
"42\n",
0,
),
# test ambiguous globs # test ambiguous globs
( (
""" """

View file

@ -792,6 +792,10 @@ class ProcProxy:
"stack": spec.stack, "stack": spec.stack,
}, },
) )
except SystemExit as e:
# the alias function is running in the main thread, so we need to
# catch SystemExit to prevent the entire shell from exiting (see #5689)
r = e.code if isinstance(e.code, int) else int(bool(e.code))
except Exception: except Exception:
xt.print_exception(source_msg="Exception in " + get_proc_proxy_name(self)) xt.print_exception(source_msg="Exception in " + get_proc_proxy_name(self))
r = 1 r = 1