From e1e769cffe380663cc75232a63f326bdb7ae0b50 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Mon, 6 Jul 2026 08:58:52 -0300 Subject: [PATCH 1/4] fix 1849: if string_render_output_form does not get a string, fall back to the default format function. --- mathics/format/form/outputform.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mathics/format/form/outputform.py b/mathics/format/form/outputform.py index 8f67b34ce..9d151cf2b 100644 --- a/mathics/format/form/outputform.py +++ b/mathics/format/form/outputform.py @@ -247,6 +247,7 @@ def render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs): """ Build a pretty-print text from an `Expression` """ + lookup_name: str format_expr: Expression = do_format(expr, evaluation, SymbolOutputForm) # type: ignore while format_expr.has_form("HoldForm", 1): # type: ignore @@ -256,7 +257,7 @@ def render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs): return "" head = format_expr.get_head() - lookup_name: str = head.get_name() or head.get_lookup_name() + lookup_name = head.get_name() or head.get_lookup_name() callback = EXPR_TO_OUTPUTFORM_TEXT_MAP.get(lookup_name, None) if callback is None: if head in evaluation.definitions.outputforms: @@ -844,6 +845,9 @@ def _slotsequence_outputform_text(expr: Expression, evaluation: Evaluation, **kw def string_render_output_form(expr: String, evaluation: Evaluation, **kwargs) -> str: from mathics.format.render.text import string as render_string + if not isinstance(expr, String): + raise _WrongFormattedExpression + # To render a string in OutputForm, we use the # function that render strings from Boxed expressions. # When a String object is converted into Boxes, From 90544cc04f4c4a8a6aa0993aecb4406571480493 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Mon, 6 Jul 2026 09:33:35 -0300 Subject: [PATCH 2/4] add details of the implementation in the module docstring --- mathics/format/form/outputform.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathics/format/form/outputform.py b/mathics/format/form/outputform.py index 9d151cf2b..5814a6388 100644 --- a/mathics/format/form/outputform.py +++ b/mathics/format/form/outputform.py @@ -3,6 +3,15 @@ OutputForm is two-dimensional keyboard-character-only output, suitable for CLI and text terminals. + +The entry point of this module is the function `render_output_form`. This function takes an expression and an evaluation object as parameters, and returns a string representing the expression in its OutputForm. + +To do this, `render_output_form` looks for the head of the expression, and uses its name to lookup one of the registered functions +to process specific kind of expressions. Callback functions are registered using the decorator `@register_outputform([lookupname])`. + +If the callback function cannot process its argument, it raises a `_WrongFormattedExpression`, to make it know `render_output_form` +it must use the default callback function. This default function is also called when an specific callback function is not available. + """ import re From c1c1ba67313db224caa6c78ed57ab730b6ed99e3 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Mon, 6 Jul 2026 17:17:50 -0300 Subject: [PATCH 3/4] Update outputform.py --- mathics/format/form/outputform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mathics/format/form/outputform.py b/mathics/format/form/outputform.py index 5814a6388..619899ba4 100644 --- a/mathics/format/form/outputform.py +++ b/mathics/format/form/outputform.py @@ -851,7 +851,7 @@ def _slotsequence_outputform_text(expr: Expression, evaluation: Evaluation, **kw @register_outputform("System`String") -def string_render_output_form(expr: String, evaluation: Evaluation, **kwargs) -> str: +def string_render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs) -> str: from mathics.format.render.text import string as render_string if not isinstance(expr, String): From 41ed1be62d82edb2dd47ef2e99caa31a3703ef86 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Mon, 6 Jul 2026 17:23:42 -0300 Subject: [PATCH 4/4] black --- mathics/format/form/outputform.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mathics/format/form/outputform.py b/mathics/format/form/outputform.py index 619899ba4..b6697d080 100644 --- a/mathics/format/form/outputform.py +++ b/mathics/format/form/outputform.py @@ -851,7 +851,9 @@ def _slotsequence_outputform_text(expr: Expression, evaluation: Evaluation, **kw @register_outputform("System`String") -def string_render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs) -> str: +def string_render_output_form( + expr: BaseElement, evaluation: Evaluation, **kwargs +) -> str: from mathics.format.render.text import string as render_string if not isinstance(expr, String):