From 20105f8fd064b122c0563bb063f77bb34f6074c1 Mon Sep 17 00:00:00 2001 From: Stanislav Date: Fri, 6 Nov 2026 23:37:45 +0400 Subject: [PATCH 1/3] CLI: an abort while reading input does not end the session The interrupt menu raises AbortInterrupt from the SIGINT handler. When the handler runs while the CLI reads input, no evaluation is there to catch it: it left interactive_eval_loop and ended the session with a traceback. Now the loop drops the input being typed and shows a new prompt. Reaching it takes a SIGINT that Python handles inside input(). With readline, Ctrl-C at the prompt does not get there: readline handles it. It does when the line was typed before readline set up the terminal, as a script driving the CLI in a pseudo-terminal does. Co-Authored-By: Claude Opus 5.5 --- mathics/repl.py | 7 ++++++ test/test_repl_abort_at_prompt.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 test/test_repl_abort_at_prompt.py diff --git a/mathics/repl.py b/mathics/repl.py index 4bed73c743..e2c38fb2d2 100644 --- a/mathics/repl.py +++ b/mathics/repl.py @@ -21,6 +21,7 @@ from mathics.core.atoms import String from mathics.core.evaluation import Evaluation, Output from mathics.core.expression import Expression +from mathics.core.interrupt import AbortInterrupt from mathics.core.parser import MathicsFileLineFeeder, MathicsLineFeeder from mathics.core.streams import stream_manager from mathics.core.symbols import SymbolNull, strip_context @@ -381,6 +382,12 @@ def interactive_eval_loop( shell.print_result(result, strict_wl_output=strict_wl_output) except KeyboardInterrupt: print("\nKeyboardInterrupt") + except AbortInterrupt: + # Ctrl-C at the prompt, then "abort" in the interrupt menu: no + # evaluation is running, so the input being typed is dropped and + # a new prompt is shown. Uncaught, it ended the session with a + # traceback. + pass except EOFError: print("\n\nGoodbye!\n") break diff --git a/test/test_repl_abort_at_prompt.py b/test/test_repl_abort_at_prompt.py new file mode 100644 index 0000000000..5e638e6850 --- /dev/null +++ b/test/test_repl_abort_at_prompt.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +""" +An abort that reaches the CLI's read-eval loop while it reads input: the +interrupt menu's "abort" with no evaluation running. The input being typed +is dropped and the session goes on with a new prompt; it used to end with an +AbortInterrupt traceback. + +The SIGINT handler runs the menu inside input(), so the AbortInterrupt comes +out of the shell's read_line(); the test makes read_line() raise it. +""" + +from mathics.core.definitions import Definitions +from mathics.core.interrupt import AbortInterrupt +from mathics.core.load_builtin import import_and_load_builtins +from mathics.repl import TerminalShell, interactive_eval_loop + + +def test_abort_while_reading_input_keeps_the_session(capsys): + import_and_load_builtins() + definitions = Definitions(add_builtin=True) + shell = TerminalShell( + definitions, "NoColor", want_readline=False, want_completion=False, autoload=False + ) + script = [AbortInterrupt(), "6*7", EOFError()] + + def read_line(prompt): + step = script.pop(0) + if isinstance(step, BaseException): + raise step + return step + + shell.read_line = read_line + interactive_eval_loop(shell, full_form=False, strict_wl_output=False, init_signal_handler=False) + out = capsys.readouterr().out + assert script == [], "the session ended before reading all its input" + assert "= 42" in out, out From 30851905c29c6cac628fbbb8616efbf7414e62d3 Mon Sep 17 00:00:00 2001 From: Stanislav Date: Sat, 7 Nov 2026 01:51:37 +0400 Subject: [PATCH 2/3] test_repl_abort_at_prompt: clear the pre-evaluation hook another test leaves installed Co-Authored-By: Claude Opus 5.5 --- test/test_repl_abort_at_prompt.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/test_repl_abort_at_prompt.py b/test/test_repl_abort_at_prompt.py index 5e638e6850..6cda4c2aa6 100644 --- a/test/test_repl_abort_at_prompt.py +++ b/test/test_repl_abort_at_prompt.py @@ -15,7 +15,11 @@ from mathics.repl import TerminalShell, interactive_eval_loop -def test_abort_while_reading_input_keeps_the_session(capsys): +def test_abort_while_reading_input_keeps_the_session(capsys, monkeypatch): + import mathics.core as mathics_core + + # test_main_preeval.py leaves a hook of its own installed + monkeypatch.setattr(mathics_core, "PRE_EVALUATION_HOOK", None) import_and_load_builtins() definitions = Definitions(add_builtin=True) shell = TerminalShell( From 61c23cf9b1f1bf3b6705ba63dd49844e48f42be2 Mon Sep 17 00:00:00 2001 From: rocky Date: Fri, 6 Nov 2026 18:58:44 -0500 Subject: [PATCH 3/3] black --- test/test_repl_abort_at_prompt.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/test_repl_abort_at_prompt.py b/test/test_repl_abort_at_prompt.py index 6cda4c2aa6..03fa5d490e 100644 --- a/test/test_repl_abort_at_prompt.py +++ b/test/test_repl_abort_at_prompt.py @@ -23,7 +23,11 @@ def test_abort_while_reading_input_keeps_the_session(capsys, monkeypatch): import_and_load_builtins() definitions = Definitions(add_builtin=True) shell = TerminalShell( - definitions, "NoColor", want_readline=False, want_completion=False, autoload=False + definitions, + "NoColor", + want_readline=False, + want_completion=False, + autoload=False, ) script = [AbortInterrupt(), "6*7", EOFError()] @@ -34,7 +38,9 @@ def read_line(prompt): return step shell.read_line = read_line - interactive_eval_loop(shell, full_form=False, strict_wl_output=False, init_signal_handler=False) + interactive_eval_loop( + shell, full_form=False, strict_wl_output=False, init_signal_handler=False + ) out = capsys.readouterr().out assert script == [], "the session ended before reading all its input" assert "= 42" in out, out