From 0b65348d5e887eebed08d53225e8b4b821916239 Mon Sep 17 00:00:00 2001 From: Andy Kipp Date: Tue, 25 Jun 2024 18:04:14 +0200 Subject: [PATCH] prompt: switching to prompt_toolkit in case of stdin with interactive mode (#5536) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Motivation There is an edge case that we're using mostly in integration tests from the beginning of the world: `echo 'echo 1' | xonsh -i` and it's not working with `TERM=dumb` because dumb is readline. So in this case we need to force using prompt_toolkit to avoid hanging. Closes #5462 #5517 ### Before These all produces hanging because reading stdin is not implemented in readline backend: ```xsh echo whoami | TERM=dumb xonsh -i --no-rc # dumb is readline in fact # or the same echo whoami | xonsh -st dumb -i # dumb is readline in fact # or the same echo whoami | xonsh -st readline -i ``` ### After Switching to prompt_toolkit in these cases. ## For community ⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍 comment** --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- news/prompt_switch_to_ptk.rst | 23 +++++++++++++++++++++++ xonsh/shell.py | 12 +++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 news/prompt_switch_to_ptk.rst diff --git a/news/prompt_switch_to_ptk.rst b/news/prompt_switch_to_ptk.rst new file mode 100644 index 000000000..dd5c1b4e7 --- /dev/null +++ b/news/prompt_switch_to_ptk.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* prompt: Switching to prompt_toolkit in edge case of sending stdin to interactive mode (#5462 #5517). + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/xonsh/shell.py b/xonsh/shell.py index f54c53509..2bab837ee 100644 --- a/xonsh/shell.py +++ b/xonsh/shell.py @@ -202,9 +202,19 @@ class Shell: if is_class(backend): cls = backend else: + """ + There is an edge case that we're using mostly in integration tests: + `echo 'echo 1' | xonsh -i` and it's not working with `TERM=dumb` (#5462 #5517) + because `dumb` is readline where stdin is not supported yet. PR is very welcome! + So in this case we need to force using prompt_toolkit. + """ + is_stdin_to_interactive = ( + XSH.env.get("XONSH_INTERACTIVE", False) and not sys.stdin.isatty() + ) + if backend == "none": from xonsh.base_shell import BaseShell as cls - elif backend == "prompt_toolkit": + elif backend == "prompt_toolkit" or is_stdin_to_interactive: from xonsh.ptk_shell.shell import PromptToolkitShell as cls elif backend == "readline": from xonsh.readline_shell import ReadlineShell as cls