Skip to content

StandardForm errors given an expression when the first argument is a String #1849

Description

@rocky

Description

When we have an Expression where the first element is a String, and other arguments, StandardForm formatting throw an unrecoverable error.

How to Reproduce and Output Given

In[1]:= xx = "a string"; StringTake[xx[0]]
StringTake::argr: StringTake called with 1 argument; 2 arguments are expected.
Traceback (most recent call last):
  File "/home/rocky/.pyenv/versions/3.13.12/bin/mathics3", line 7, in <module>
    sys.exit(main())
             ~~~~^^
  File "tmp/Mathics3/mathics-core/mathics/__main__.py", line 632, in main
    interactive_eval_loop(shell, args.full_form, args.strict_wl_output)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "tmp/Mathics3/mathics-core/mathics/__main__.py", line 372, in interactive_eval_loop
    result = evaluation.evaluate(query, timeout=settings.TIMEOUT)
  File "tmp/Mathics3/mathics-core/mathics/core/evaluation.py", line 277, in evaluate
    result = evaluate()
  File "tmp/Mathics3/mathics-core/mathics/core/evaluation.py", line 270, in evaluate
    return self.format_output(self.last_eval, format)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "tmp/Mathics3/mathics-core/mathics/core/evaluation.py", line 406, in format_output
    result = format_element(expr, self, SymbolOutputForm)
  File "tmp/Mathics3/mathics-core/mathics/format/box/makeboxes.py", line 299, in format_element
    result_box = apply_makeboxes_rules(formatted_expr, evaluation, form)
  File "tmp/Mathics3/mathics-core/mathics/format/box/makeboxes.py", line 127, in apply_makeboxes_rules
    return eval_generic_makeboxes(expr, form, evaluation)
  File "tmp/Mathics3/mathics-core/mathics/format/box/makeboxes.py", line 227, in eval_generic_makeboxes
    return printform_callback(elements[0], evaluation)
  File "tmp/Mathics3/mathics-core/mathics/builtin/forms/print.py", line 260, in eval_makeboxes_outputform
    text_outputform = str(render_output_form(expr, evaluation, **kwargs))
                          ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "tmp/Mathics3/mathics-core/mathics/format/form/outputform.py", line 267, in render_output_form
    result = callback(format_expr, evaluation, **kwargs)
  File "tmp/Mathics3/mathics-core/mathics/format/form/outputform.py", line 107, in _default_render_output_form
    elements = [render_output_form(elem, evaluation) for elem in expr.elements]
                ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "tmp/Mathics3/mathics-core/mathics/format/form/outputform.py", line 267, in render_output_form
    result = callback(format_expr, evaluation, **kwargs)
  File "tmp/Mathics3/mathics-core/mathics/format/form/outputform.py", line 862, in string_render_output_form
    return render_string(expr, **kwargs)
  File "tmp/Mathics3/mathics-core/mathics/format/render/text.py", line 167, in string
    if value.startswith('"') and value.endswith('"'):  # nopep8
       ^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'startswith'
08

Expected behavior

No error

Workarounds

#1848 is a workaround.

Priority

Nothing special; just something noticed.

Additional context

The problem stems from the fact that we have:

<Expression: <String: "a string">[<Integer: 0>]>>

And formatting finds the head to be a String and formats as though the Expression is a string.
Expressions do not have a ".value" attribute, or it is None.

This is the code in render_output_form of mathics.format.form.outputform that is wrong:

def render_output_form(expr: BaseElement, evaluation: Evaluation, **kwargs):
    """
    Build a pretty-print text from an `Expression`
    """
...    
    head = format_expr.get_head()
    lookup_name: str = head.get_name() or head.get_lookup_name()
    callback = EXPR_TO_OUTPUTFORM_TEXT_MAP.get(lookup_name, None)

but I don't know what the right thing to do here would be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions