Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion mathics/builtin/atomic/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,20 @@ def eval_default(self, value, evaluation: Evaluation, options: dict):
def eval_form(self, expr, form, evaluation: Evaluation, options: dict):
"ToString[expr_, form_Symbol, OptionsPattern[ToString]]"
encoding = options["System`CharacterEncoding"]
return eval_ToString(expr, form, encoding.value, evaluation)
if isinstance(encoding, String):
encoding_str = encoding.value
if encoding_str not in _encodings:
evaluation.message("$CharacterEncoding", "charcode", encoding)
encoding_str = evaluation.definitions.get_ownvalue(
"System`$SystemCharacterEncoding"
).value
else:
evaluation.message("$CharacterEncoding", "charcode", encoding)
encoding_str = evaluation.definitions.get_ownvalue(
"System`$SystemCharacterEncoding"
).value

return eval_ToString(expr, form, encoding_str, evaluation)


class Transliterate(Builtin):
Expand Down
4 changes: 2 additions & 2 deletions mathics/eval/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
def eval_ToString(
expr: BaseElement, form: Symbol, encoding: String, evaluation: Evaluation
) -> String:
boxes = format_element(expr, evaluation, form, encoding=encoding)
text = boxes.to_text(evaluation=evaluation)
boxes = format_element(expr, evaluation, form)
text = boxes.to_text(evaluation=evaluation, encoding=encoding)
return String(text)


Expand Down
41 changes: 40 additions & 1 deletion mathics/format/render/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""
Mathics3 box rendering to plain text.
"""

from mathics.builtin.box.graphics import GraphicsBox
from mathics.builtin.box.graphics3d import Graphics3DBox
from mathics.builtin.box.layout import (
Expand Down Expand Up @@ -34,6 +33,43 @@
add_render_function(FormBox, convert_inner_box_field)


# Map WMA encoding names to Python encoding names
ENCODING_WMA_TO_PYTHON = {
"WindowsEastEurope": "cp1250",
"WindowsCyrillic": "cp1251",
"WindowsANSI": "cp1252",
"WindowsGreek": "cp1252",
"WindowsTurkish": "cp1254",
}


def encode_string_value(value: str, encoding: str):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is a just a proof of concept. The final version should look into the MathicsScanner tables

"""Convert an Unicode string `value` to the required `encoding`"""
if encoding == "ASCII":
# TODO: replace from a table from MathicsScanner
ascii_map = {
"⇒": "=>",
"↔": "<->",
"→": "->",
"⇾": "->",
"⇾": "->",
"⇴": "->",
"∫": r"\[Integral]",
"𝑑": r"\[DifferentialD]",
"⧦": r"\[Equivalent]",
"×": r" x ",
}
result = ""
for ch in value:
ch = ascii_map.get(ch, ch)
result += ch
return result

encoding = ENCODING_WMA_TO_PYTHON.get(encoding, encoding)
result = value.encode("utf-8").decode(encoding)
return result


def fractionbox(box: FractionBox, **options) -> str:
# Note: values set in `options` take precedence over `box_options`
child_options = {**options, **box.box_options}
Expand Down Expand Up @@ -159,6 +195,9 @@ def string(s: String, **options) -> str:
if value.startswith('"') and value.endswith('"'): # nopep8
if not show_string_characters:
value = value[1:-1]

if "encoding" in options and options["encoding"] != "Unicode":
value = encode_string_value(value, options["encoding"])
Copy link
Member

@rocky rocky Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this more closely, there may be a deeper problem here.

If the Mathics3 string was encoded with Unicode under the user's control, that should remain. If Mathics3 added the Unicode because an operator appeared, that is probably wrong, and the code that added the Unicode should be fixed.

So, what is a specific scenario or situation where line 200 is triggered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 200 is triggered when the required encoding is not the standard Unicode. It happens when the SystemCharacterEncoding is not Unicode (for example by setting MATHICS_CHARACTER_ENCODING="ASCII") or when it is call from ToString with a specific CharacterEncoding option.

Copy link
Member

@rocky rocky Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 200 is triggered when the required encoding is not the standard Unicode. It happens when the SystemCharacterEncoding is not Unicode (for example by setting MATHICS_CHARACTER_ENCODING="ASCII") or when it is call from ToString with a specific CharacterEncoding option.

This paraphrases the if condition. I meant, what is it that is causing an operator to get converted before ToString was called. This, I think, is the real source of the problem.

return value


Expand Down
Loading