Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mathics/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions test/test_repl_abort_at_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- 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, 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(
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
Loading