diff --git a/mathics/interrupt.py b/mathics/interrupt.py index c47470565..0fdee00b7 100644 --- a/mathics/interrupt.py +++ b/mathics/interrupt.py @@ -16,6 +16,26 @@ from mathics.eval.stackframe import find_Mathics3_evaluation_method, get_eval_Expression +# The state of an evaluation that Evaluation.evaluate() resets and leaves +# behind (it ends with stop()). The inspect dialog evaluates its input with +# the evaluation that was interrupted, so it puts these back afterwards: +# otherwise the interrupted evaluation, once resumed, stops with "Timeout +# reached" at its next check. +INTERRUPTED_STATE = ( + "start_time", + "iteration_count", + "recursion_depth", + "timeout", + "stopped", + "deadline", + "exc_result", + "last_eval", + "predetermined_out", + "compound_out_depth", + "out", +) + + # See also __main__'s interactive_eval_loop def inspect_eval_loop(evaluation: Evaluation): """ @@ -28,7 +48,11 @@ def inspect_eval_loop(evaluation: Evaluation): else: was_inside_interrupt = False - previous_recursion_depth = evaluation.recursion_depth + interrupted = { + name: getattr(evaluation, name) + for name in INTERRUPTED_STATE + if hasattr(evaluation, name) + } while True: try: # Reset line number within an In[] line number. @@ -70,7 +94,8 @@ def inspect_eval_loop(evaluation: Evaluation): # raise to pass the error code on, e.g. Quit[1] raise finally: - evaluation.recursion_depth = previous_recursion_depth + for name, value in interrupted.items(): + setattr(evaluation, name, value) if shell is not None: shell.is_inside_interrupt = was_inside_interrupt diff --git a/test/test_interrupt_inspect.py b/test/test_interrupt_inspect.py new file mode 100644 index 000000000..4a991f6da --- /dev/null +++ b/test/test_interrupt_inspect.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +The interrupt menu's "inspect" dialog evaluates its input with the +evaluation that was interrupted. Leaving the dialog (Ctrl-D) must give that +evaluation back as it was, so that it goes on; it used to stop at its next +check with "Timeout reached" and give $Aborted. +""" + +import pytest + +from mathics.core.definitions import Definitions +from mathics.core.evaluation import Evaluation +from mathics.core.load_builtin import import_and_load_builtins +from mathics.interrupt import inspect_eval_loop +from mathics.repl import TerminalShell + + +def test_leaving_the_dialog_resumes_the_interrupted_evaluation(capsys): + import_and_load_builtins() + definitions = Definitions(add_builtin=True) + shell = TerminalShell( + definitions, "NoColor", want_readline=False, want_completion=False, autoload=False + ) + script = ["1+1", EOFError()] + + def read_line(prompt): + step = script.pop(0) + if isinstance(step, BaseException): + raise step + return step + + shell.read_line = read_line + evaluation = Evaluation(definitions) + evaluation.shell = shell + # The interrupted evaluation, as it is while it runs. + evaluation.stopped = False + evaluation.timeout = False + evaluation.deadline = None + evaluation.recursion_depth = 7 + pending = evaluation.out + + with pytest.raises(EOFError): # Ctrl-D: the menu takes it as "leave the dialog" + inspect_eval_loop(evaluation) + + assert "= 2" in capsys.readouterr().out, "the dialog did not evaluate 1+1" + assert evaluation.stopped is False + assert evaluation.recursion_depth == 7 + assert evaluation.out is pending + evaluation.check_stopped() # must not raise TimeoutInterrupt