Skip to content
Merged
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
9 changes: 6 additions & 3 deletions mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,12 @@ def print_out(self, text) -> None:

if self.definitions.trace_evaluation:
self.definitions.trace_evaluation = False
text = self.format_output(from_python(text), "text")
self.is_boxing = False
self.definitions.trace_evaluation = True
# An interrupt while formatting must not turn tracing off for good.
try:
text = self.format_output(from_python(text), "text")
finally:
self.is_boxing = False
self.definitions.trace_evaluation = True
else:
text = self.format_output(from_python(text), "text")

Expand Down
30 changes: 22 additions & 8 deletions mathics/eval/drawing/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,14 @@ def quiet_f(*args):
arg_name: Integer(arg) if isinstance(arg, int) else Real(arg)
for arg_name, arg in zip(arg_names, args)
}
value = dynamic_scoping(expr.evaluate, vars, evaluation)
if isinstance(value, Expression):
# Rationals can appear like this.
value = eval_N(value, evaluation)
evaluation.quiet_all = old_quiet_all
# An interrupt (a timeout, Abort[]) must not leave the session quiet.
try:
value = dynamic_scoping(expr.evaluate, vars, evaluation)
if isinstance(value, Expression):
# Rationals can appear like this.
value = eval_N(value, evaluation)
finally:
evaluation.quiet_all = old_quiet_all
if expect_list:
if value.has_form(SymbolList, None):
value = [extract_pyreal(item) for item in value.elements]
Expand Down Expand Up @@ -481,6 +484,20 @@ def eval_ListPlot(

@Timer("eval_Plot")
def eval_Plot(plot_options, options: dict, evaluation: Evaluation) -> Expression:
"""
Evaluation part of Plot[], with messages turned off while the functions
are sampled.
"""
prev_quiet_all, evaluation.quiet_all = evaluation.quiet_all, True
# An interrupt (a timeout, Abort[]) must not leave the session quiet;
# it would also hide the General::timeout of this very evaluation.
try:
return _eval_Plot(plot_options, options, evaluation)
finally:
evaluation.quiet_all = prev_quiet_all


def _eval_Plot(plot_options, options: dict, evaluation: Evaluation) -> Expression:
"""
Evaluation part of Plot[]

Expand Down Expand Up @@ -537,7 +554,6 @@ def get_points_range(points):
# like the Hue.
graphics = []

prev_quiet_all, evaluation.quiet_all = evaluation.quiet_all, True
for index, f in enumerate(functions):
points = []
xvalues = [] # x value for each point in points
Expand Down Expand Up @@ -708,8 +724,6 @@ def find_excl(excl):
mesh_points = [to_mathics_list(xx, yy) for xx, yy in points]
graphics.append(Expression(SymbolPoint, ListExpression(*mesh_points)))

# Restore the quiet_all state
evaluation.quiet_all = prev_quiet_all
return Expression(
SymbolGraphics,
ListExpression(*graphics),
Expand Down
10 changes: 6 additions & 4 deletions mathics/eval/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ def wrapper(expr, evaluation) -> Any:
# boxing temporarily.
phase_name = "Rewriting" if is_performing_rewrite(func) else "Evaluating"
evaluation.is_boxing = True
trace_evaluate_action = trace_evaluate_on_call(
expr, evaluation, phase_name, func
)
evaluation.is_boxing = was_boxing
try:
trace_evaluate_action = trace_evaluate_on_call(
expr, evaluation, phase_name, func
)
finally:
evaluation.is_boxing = was_boxing
if trace_evaluate_action is None:
result = func(expr, evaluation)
if trace_evaluate_on_return is not None and not was_boxing:
Expand Down
32 changes: 17 additions & 15 deletions mathics/format/box/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,24 @@ def format_element(
"""
was_boxing = evaluation.is_boxing
evaluation.is_boxing = True
# InputForm and OutputForm do format in the MakeBoxes implementation.
if form in (SymbolInputForm, SymbolOutputForm):
formatted_expr = element
else:
formatted_expr = do_format(element, evaluation, form)

if form not in evaluation.definitions.boxforms:
formatted_expr = Expression(form, formatted_expr)
form = SymbolStandardForm
result_box = apply_makeboxes_rules(formatted_expr, evaluation, form)
if isinstance(result_box, BoxElementMixin):
# An interrupt (a timeout, Abort[]) while formatting must not leave the
# session boxing: TraceEvaluation would stay off.
try:
# InputForm and OutputForm do format in the MakeBoxes implementation.
if form in (SymbolInputForm, SymbolOutputForm):
formatted_expr = element
else:
formatted_expr = do_format(element, evaluation, form)

if form not in evaluation.definitions.boxforms:
formatted_expr = Expression(form, formatted_expr)
form = SymbolStandardForm
result_box = apply_makeboxes_rules(formatted_expr, evaluation, form)
if isinstance(result_box, BoxElementMixin):
return result_box
return eval_makeboxes_fullform_recursive(element, evaluation)
finally:
evaluation.is_boxing = was_boxing
return result_box
result = eval_makeboxes_fullform_recursive(element, evaluation)
evaluation.is_boxing = was_boxing
return result


def to_boxes(
Expand Down
17 changes: 17 additions & 0 deletions test/builtin/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ def test_evaluate_timeout(str_expr):
assert session.evaluate_as_in_cli("Length[Table[i, {i, 10}]]").result == "10"


def test_evaluate_timeout_restores_state():
"""A timeout inside Plot, which turns messages off while it samples, and
inside the formatting of its result, gives General::timeout and leaves
neither the session quiet nor boxing."""
from test.helper import session

result = session.evaluate_as_in_cli(
"Plot[(Do[k, {k, 10^4}]; x), {x, 0, 1}]", timeout=0.3
)
assert result.result == "$Aborted"
assert [m.text for m in result.out] == ["Timeout reached."]
assert session.evaluation.quiet_all is False
assert session.evaluation.is_boxing is False
result = session.evaluate_as_in_cli("1/0")
assert [m.text for m in result.out] == ["Infinite expression 1 / 0 encountered."]


def test_infinite_recursion_limit(monkeypatch):
"""$RecursionLimit = Infinity goes as deep as MAX_INFINITE_RECURSION_DEPTH,
then stops, and the message gives the limit in effect inside the Block."""
Expand Down
Loading