diff --git a/mathics/core/evaluation.py b/mathics/core/evaluation.py index ac8c3cf2a..76c76e7f4 100644 --- a/mathics/core/evaluation.py +++ b/mathics/core/evaluation.py @@ -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") diff --git a/mathics/eval/drawing/plot.py b/mathics/eval/drawing/plot.py index f641f8c14..d41df1f0d 100644 --- a/mathics/eval/drawing/plot.py +++ b/mathics/eval/drawing/plot.py @@ -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] @@ -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[] @@ -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 @@ -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), diff --git a/mathics/eval/tracing.py b/mathics/eval/tracing.py index 997598a80..2f4278bca 100644 --- a/mathics/eval/tracing.py +++ b/mathics/eval/tracing.py @@ -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: diff --git a/mathics/format/box/makeboxes.py b/mathics/format/box/makeboxes.py index 70059ab7a..c51e58ec8 100644 --- a/mathics/format/box/makeboxes.py +++ b/mathics/format/box/makeboxes.py @@ -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( diff --git a/test/builtin/test_evaluation.py b/test/builtin/test_evaluation.py index 8f9c32872..49565af92 100644 --- a/test/builtin/test_evaluation.py +++ b/test/builtin/test_evaluation.py @@ -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."""