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
29 changes: 27 additions & 2 deletions mathics/interrupt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be an Enumeration

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sense I am getting is that AI is good with several things, but it can use or tolerate older forms of Python, and also does not pick up that well on the existing style and practice of existing code (unless explicitly directed to). I suspect this is also why we see so many isort/black failures initially as well.

# 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):
"""
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
49 changes: 49 additions & 0 deletions test/test_interrupt_inspect.py
Original file line number Diff line number Diff line change
@@ -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
Loading