From aca9b16d46379554ea961c3595c1380fc3f80d90 Mon Sep 17 00:00:00 2001 From: mmatera Date: Sun, 13 Nov 2022 13:47:23 -0300 Subject: [PATCH 1/3] using a private symbol to store the state. --- mathics/builtin/forms/output.py | 33 ++++++++++++++++++++++++++++----- mathics/builtin/makeboxes.py | 9 ++++++++- mathics/core/evaluation.py | 2 -- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/mathics/builtin/forms/output.py b/mathics/builtin/forms/output.py index bdbb28f8e..269e96e10 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_FORMATBOXES, number_form from mathics.builtin.tensors import get_dimensions from mathics.core.atoms import ( @@ -188,12 +188,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_FORMATBOXES.evaluate(evaluation) + evaluation.definitions.set_ownvalue(SYMBOL_FORMATBOXES.name, SymbolTrue) try: result = MakeBoxes(expr, f).evaluate(evaluation) - except: - pass - evaluation.in_display_form = previous_df + except Exception: + return + evaluation.definitions.set_ownvalue( + SYMBOL_FORMATBOXES.name, old_value_in_display_form + ) return result @@ -1124,3 +1128,22 @@ 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. +
+ """ + + name = "PrivateSymbolThatControlsTheStateOfDisplayForm" + context = "System`Private`MakeBoxes`" + summary_text = "control if boxes are reevaluated." + rules = { + "System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm": "False", + } diff --git a/mathics/builtin/makeboxes.py b/mathics/builtin/makeboxes.py index 84d504c1a..de78c8e9f 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_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_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 From 475ef2684a286f50061df17dc61e7ce01a924e8b Mon Sep 17 00:00:00 2001 From: mmatera Date: Sun, 13 Nov 2022 13:53:11 -0300 Subject: [PATCH 2/3] lock and protect the variable --- mathics/builtin/forms/output.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mathics/builtin/forms/output.py b/mathics/builtin/forms/output.py index 269e96e10..e396b16cc 100644 --- a/mathics/builtin/forms/output.py +++ b/mathics/builtin/forms/output.py @@ -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 @@ -194,7 +195,7 @@ def eval_makeboxes(self, expr, f, evaluation): try: result = MakeBoxes(expr, f).evaluate(evaluation) except Exception: - return + result = None evaluation.definitions.set_ownvalue( SYMBOL_FORMATBOXES.name, old_value_in_display_form ) @@ -1141,9 +1142,10 @@ class PrivateSymbolThatControlsTheStateOfDisplayForm(Builtin): """ - name = "PrivateSymbolThatControlsTheStateOfDisplayForm" + attributes = A_LOCKED | A_PROTECTED context = "System`Private`MakeBoxes`" - summary_text = "control if boxes are reevaluated." + name = "PrivateSymbolThatControlsTheStateOfDisplayForm" rules = { "System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm": "False", } + summary_text = "control if boxes are reevaluated." From 8fd073e7b6d128c95bcac8ba5089d6b17a5a270e Mon Sep 17 00:00:00 2001 From: mmatera Date: Sun, 13 Nov 2022 14:04:35 -0300 Subject: [PATCH 3/3] fix name --- mathics/builtin/forms/output.py | 8 ++++---- mathics/builtin/makeboxes.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mathics/builtin/forms/output.py b/mathics/builtin/forms/output.py index e396b16cc..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, SYMBOL_FORMATBOXES, 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 ( @@ -190,14 +190,14 @@ def eval_makeboxes(self, expr, f, evaluation): # Hopefully, this is temporal and it is not going to be # needed after the Format/MakeBoxes refactor. - old_value_in_display_form = SYMBOL_FORMATBOXES.evaluate(evaluation) - evaluation.definitions.set_ownvalue(SYMBOL_FORMATBOXES.name, SymbolTrue) + 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 Exception: result = None evaluation.definitions.set_ownvalue( - SYMBOL_FORMATBOXES.name, old_value_in_display_form + SYMBOL_NO_FORMATBOXES.name, old_value_in_display_form ) return result diff --git a/mathics/builtin/makeboxes.py b/mathics/builtin/makeboxes.py index de78c8e9f..fcbe4108d 100644 --- a/mathics/builtin/makeboxes.py +++ b/mathics/builtin/makeboxes.py @@ -48,7 +48,7 @@ # Afterwards, we can find a better name -SYMBOL_FORMATBOXES = Symbol( +SYMBOL_NO_FORMATBOXES = Symbol( "System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm" ) @@ -403,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 SYMBOL_FORMATBOXES.evaluate(evaluation) is SymbolTrue: + if SYMBOL_NO_FORMATBOXES.evaluate(evaluation) is SymbolTrue: return expr expr = expr.to_expression() if isinstance(expr, Atom):