diff --git a/mathics/builtin/forms/output.py b/mathics/builtin/forms/output.py index bdbb28f8e..32aa93a64 100644 --- a/mathics/builtin/forms/output.py +++ b/mathics/builtin/forms/output.py @@ -18,7 +18,7 @@ from mathics.builtin.box.layout import GridBox, RowBox, to_boxes from mathics.builtin.comparison import expr_min from mathics.builtin.forms.base import FormBaseClass -from mathics.builtin.makeboxes import MakeBoxes, number_form +from mathics.builtin.makeboxes import MakeBoxes, SYMBOL_NO_FORMATBOXES, number_form from mathics.builtin.tensors import get_dimensions from mathics.core.atoms import ( @@ -30,6 +30,7 @@ StringFromPython, ) +from mathics.core.attributes import A_LOCKED, A_PROTECTED from mathics.core.expression import Expression, BoxError from mathics.core.formatter import format_element from mathics.core.list import ListExpression @@ -188,12 +189,16 @@ def eval_makeboxes(self, expr, f, evaluation): # MakeBoxes is formatting an expression inside a DisplayForm. # Hopefully, this is temporal and it is not going to be # needed after the Format/MakeBoxes refactor. - previous_df, evaluation.in_display_form = evaluation.in_display_form, True + + old_value_in_display_form = SYMBOL_NO_FORMATBOXES.evaluate(evaluation) + evaluation.definitions.set_ownvalue(SYMBOL_NO_FORMATBOXES.name, SymbolTrue) try: result = MakeBoxes(expr, f).evaluate(evaluation) - except: - pass - evaluation.in_display_form = previous_df + except Exception: + result = None + evaluation.definitions.set_ownvalue( + SYMBOL_NO_FORMATBOXES.name, old_value_in_display_form + ) return result @@ -1124,3 +1129,23 @@ def eval_makeboxes_matrix(self, table, f, evaluation, options): return RowBox(String("("), result, String(")")) return result + + +# Custom private symbol that helps DisplayForm to control if +# MakeBoxes must process BoxElements as expressions, or keep as they are. +class PrivateSymbolThatControlsTheStateOfDisplayForm(Builtin): + """ +
+
'PrivateSymbolThatControlsTheStateOfDisplayForm' +
If True, MakeBoxes keep Box elements as they are. Otherwise,\ + tries to format them as if it were expressions. +
+ """ + + attributes = A_LOCKED | A_PROTECTED + context = "System`Private`MakeBoxes`" + name = "PrivateSymbolThatControlsTheStateOfDisplayForm" + rules = { + "System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm": "False", + } + summary_text = "control if boxes are reevaluated." diff --git a/mathics/builtin/makeboxes.py b/mathics/builtin/makeboxes.py index 84d504c1a..fcbe4108d 100644 --- a/mathics/builtin/makeboxes.py +++ b/mathics/builtin/makeboxes.py @@ -37,6 +37,7 @@ from mathics.core.symbols import ( Atom, Symbol, + SymbolTrue, ) from mathics.core.systemsymbols import ( @@ -46,6 +47,12 @@ ) +# Afterwards, we can find a better name +SYMBOL_NO_FORMATBOXES = Symbol( + "System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm" +) + + def int_to_s_exp(expr, n): n = expr.get_int_value() if n < 0: @@ -396,7 +403,7 @@ def apply_general(self, expr, f, evaluation): if isinstance(expr, BoxElementMixin): # If we are inside a DisplayForm block, # BoxElementMixin are not processed. - if evaluation.in_display_form: + if SYMBOL_NO_FORMATBOXES.evaluate(evaluation) is SymbolTrue: return expr expr = expr.to_expression() if isinstance(expr, Atom): diff --git a/mathics/core/evaluation.py b/mathics/core/evaluation.py index fd6356dbf..c08e6c510 100644 --- a/mathics/core/evaluation.py +++ b/mathics/core/evaluation.py @@ -257,8 +257,6 @@ def __init__( self.quiet_all = False self.format = format - # See comment in mathics.builtin.makeboxes.DisplayForm - self.in_display_form = False self.catch_interrupt = catch_interrupt self.SymbolNull = SymbolNull