Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e02e3a9
Improve use of CharacterEncoding
mmatera Mar 15, 2026
586d3a4
Merge branch 'master' into fix_ToStringEncoding
mmatera Mar 15, 2026
96ea4e8
Merge branch 'master' into fix_ToStringEncoding
mmatera Mar 16, 2026
cd526f5
Merge branch 'master' into fix_ToStringEncoding
rocky Mar 20, 2026
dc9c8ad
Merge remote-tracking branch 'origin/master' into fix_ToStringEncoding
mmatera Mar 24, 2026
1a53c1a
not finished
mmatera Mar 25, 2026
3d4b0a5
hangle encoding in doctests
mmatera Mar 25, 2026
0218bd9
adjust tests
mmatera Mar 25, 2026
1324c41
commenting out the Mathml tests
mmatera Mar 25, 2026
f58574c
Merge remote-tracking branch 'origin/master' into fix_ToStringEncoding
mmatera Mar 25, 2026
79dcf9d
adding missing module
mmatera Mar 25, 2026
e7f88e5
avoid circular import
mmatera Mar 25, 2026
0595532
using Mathics3-scanner tables. Moving encoding.py to mathics.eval
mmatera Mar 25, 2026
1670da6
remove hard coded table
mmatera Mar 25, 2026
d9e7eb5
last tweaks
mmatera Mar 27, 2026
b01006c
Merge branch 'master' into fix_ToStringEncoding
mmatera Mar 29, 2026
8a33df5
Merge branch 'master' into fix_ToStringEncoding
mmatera Mar 30, 2026
2d82c6d
Merge branch 'master' into fix_ToStringEncoding
mmatera Apr 3, 2026
68cb8b9
parent 6239c8346809177f6497fc594e4cc20b0c9d9686
mmatera Mar 29, 2026
cd3cf0d
parent 6239c8346809177f6497fc594e4cc20b0c9d9686
mmatera Mar 29, 2026
b743191
strip result before the comparison
mmatera Apr 4, 2026
ba5d790
fix wrong character
mmatera Apr 4, 2026
17ab410
Merge remote-tracking branch 'origin/master' into fix_ToStringEncoding
mmatera Apr 4, 2026
55ac83b
merge with handle_encoding_in_docpipeline
mmatera Apr 4, 2026
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
6 changes: 3 additions & 3 deletions mathics/builtin/arithfns/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class Plus(InfixOperator, SympyFunction):

'Plus' has default value 0:
>> DefaultValues[Plus]
= {HoldPattern[Default[Plus]] :> 0}
= {HoldPattern[Default[Plus]] 0}
>> a /. n_. + x_ :> {n, x}
= {0, a}

Expand Down Expand Up @@ -326,7 +326,7 @@ class Power(InfixOperator, MPMathFunction):

'Power' has default value 1 for its second argument:
>> DefaultValues[Power]
= {HoldPattern[Default[Power, 2]] :> 1}
= {HoldPattern[Default[Power, 2]] 1}
>> a /. x_ ^ n_. :> {x, n}
= {a, 1}

Expand Down Expand Up @@ -537,7 +537,7 @@ class Times(InfixOperator, SympyFunction):

'Times' has default value 1:
>> DefaultValues[Times]
= {HoldPattern[Default[Times]] :> 1}
= {HoldPattern[Default[Times]] 1}
>> a /. n_. * x_ :> {n, x}
= {1, a}
"""
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class Boole(Builtin):
>> Boole[7 < 5]
= 0
>> Boole[a == 7]
= Boole[a == 7]
= Boole[a 7]
"""

attributes = A_LISTABLE | A_PROTECTED
Expand Down
8 changes: 4 additions & 4 deletions mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Set(InfixOperator):

An assignment like this creates an ownvalue:
>> OwnValues[a]
= {HoldPattern[a] :> 3}
= {HoldPattern[a] 3}

You can set multiple values at once using lists:
>> {a, b, c} = {10, 2, 3}
Expand Down Expand Up @@ -246,7 +246,7 @@ class TagSet(Builtin):
= {}

>> UpValues[square]
= {HoldPattern[area[square[s_]]] :> s ^ 2}
= {HoldPattern[area[square[s_]]] s ^ 2}

The symbol $f$ must appear as the ultimate head of $lhs$ or as the head \
of an element in $lhs$:
Expand Down Expand Up @@ -341,7 +341,7 @@ class UpSet(InfixOperator):
>> DownValues[a]
= {}
>> UpValues[b]
= {HoldPattern[a[b]] :> 3}
= {HoldPattern[a[b]] 3}

You can use 'UpSet' to specify special values like format values.
However, these values will not be saved in 'UpValues':
Expand Down Expand Up @@ -388,7 +388,7 @@ class UpSetDelayed(UpSet):
>> a[b]
= 2
>> UpValues[b]
= {HoldPattern[a[b]] :> x}
= {HoldPattern[a[b]] x}
"""

attributes = A_HOLD_ALL | A_PROTECTED | A_SEQUENCE_HOLD
Expand Down
10 changes: 5 additions & 5 deletions mathics/builtin/assignments/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DefaultValues(Builtin):
>> Default[f, 1] = 4
= 4
>> DefaultValues[f]
= {HoldPattern[Default[f, 1]] :> 4}
= {HoldPattern[Default[f, 1]] 4}

You can assign values to 'DefaultValues':
>> DefaultValues[g] = {Default[g] -> 3};
Expand Down Expand Up @@ -71,7 +71,7 @@ class Messages(Builtin):
>> a::b = "foo"
= foo
>> Messages[a]
= {HoldPattern[a::b] :> foo}
= {HoldPattern[a::b] foo}
>> Messages[a] = {a::c :> "bar"};
>> a::c // InputForm
= "bar"
Expand Down Expand Up @@ -105,7 +105,7 @@ class NValues(Builtin):
= {}
>> N[a] = 3;
>> NValues[a]
= {HoldPattern[N[a, MachinePrecision]] :> 3}
= {HoldPattern[N[a, MachinePrecision]] 3}

You can assign values to 'NValues':
>> NValues[b] := {N[b, MachinePrecision] :> 2}
Expand All @@ -124,7 +124,7 @@ class NValues(Builtin):
inappropriate rules will never be used:
>> NValues[d] = {foo -> bar};
>> NValues[d]
= {HoldPattern[foo] :> bar}
= {HoldPattern[foo] bar}
>> N[d]
= d
"""
Expand Down Expand Up @@ -153,7 +153,7 @@ class SubValues(Builtin):
>> f[1][x_] := x
>> f[2][x_] := x ^ 2
>> SubValues[f]
= {HoldPattern[f[2][x_]] :> x ^ 2, HoldPattern[f[1][x_]] :> x}
= {HoldPattern[f[2][x_]] x ^ 2, HoldPattern[f[1][x_]] x}
>> Definition[f]
= f[2][x_] = x^2
.
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/assignments/upvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class UpValues(Builtin):
>> a + b ^= 2
= 2
>> UpValues[a]
= {HoldPattern[a + b] :> 2}
= {HoldPattern[a + b] 2}
>> UpValues[b]
= {HoldPattern[a + b] :> 2}
= {HoldPattern[a + b] 2}

You can assign values to 'UpValues':
>> UpValues[pi] := {Sin[pi] :> 0}
Expand Down
19 changes: 16 additions & 3 deletions mathics/builtin/atomic/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ class ToString(Builtin):
"""

options = {
"CharacterEncoding": '"Unicode"',
"CharacterEncoding": "$CharacterEncoding",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For ToString, the default CharacterEncoding should be $CharacterEncoding

"FormatType": "OutputForm",
"NumberMarks": "$NumberMarks",
"PageHeight": "Infinity",
Expand All @@ -898,8 +898,21 @@ 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)
encoding = options["System`CharacterEncoding"].evaluate(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
22 changes: 11 additions & 11 deletions mathics/builtin/atomic/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class Definition(Builtin):
.
. Default[r, 1] = 2
.
.Options[r] = {Opt -> 3}
.Options[r] = {Opt 3}
.

For 'ReadProtected' symbols, 'Definition' just prints attributes, default values and options:
Expand All @@ -286,7 +286,7 @@ class Definition(Builtin):
.
. Default[r, 1] = 2
.
. Options[r] = {Opt -> 3}
. Options[r] = {Opt 3}
This is the same for built-in symbols:
>> Definition[Plus]
= Attributes[Plus] = {Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}
Expand All @@ -295,7 +295,7 @@ class Definition(Builtin):
>> Definition[Level]
= Attributes[Level] = {Protected}
.
. Options[Level] = {Heads -> False}
. Options[Level] = {Heads False}

'ReadProtected' can be removed, unless the symbol is locked:
>> ClearAttributes[r, ReadProtected]
Expand All @@ -306,7 +306,7 @@ class Definition(Builtin):
.
. Default[r, 1] = 2
.
. Options[r] = {Opt -> 3}
. Options[r] = {Opt 3}
'ClearAll' clears everything:
>> ClearAll[r]
>> Definition[r]
Expand Down Expand Up @@ -359,15 +359,15 @@ class DownValues(Builtin):

>> f[x_] := x ^ 2
>> DownValues[f]
= {HoldPattern[f[x_]] :> x ^ 2}
= {HoldPattern[f[x_]] x ^ 2}

Mathics will sort the rules you assign to a symbol according to \
their specificity. If it cannot decide which rule is more special, \
the newer one will get higher precedence.
>> f[x_Integer] := 2
>> f[x_Real] := 3
>> DownValues[f]
= {HoldPattern[f[x_Real]] :> 3, HoldPattern[f[x_Integer]] :> 2, HoldPattern[f[x_]] :> x ^ 2}
= {HoldPattern[f[x_Real]] 3, HoldPattern[f[x_Integer]] 2, HoldPattern[f[x_]] x ^ 2}
>> f[3]
= 2
>> f[3.]
Expand Down Expand Up @@ -417,11 +417,11 @@ class FormatValues(Builtin):
Now, to see the rules, we can use 'FormatValues':

>> FormatValues[F]
= {HoldPattern[Subscript[x_, F]] :> Subscript[x, F]}
= {HoldPattern[Subscript[x_, F]] Subscript[x, F]}

The replacment pattern on the right in the delayed rule is formatted according to the top-level form. To see the rule input, we can use 'InputForm':
>> FormatValues[F] //InputForm
= {HoldPattern[Format[F[x_], OutputForm]] :> Subscript[x, F]}
= {HoldPattern[Format[F[x_], OutputForm]] Subscript[x, F]}
"""

summary_text = (
Expand Down Expand Up @@ -617,13 +617,13 @@ class OwnValues(Builtin):
>> x = 3;
>> x = 2;
>> OwnValues[x]
= {HoldPattern[x] :> 2}
= {HoldPattern[x] 2}
>> x := y
>> OwnValues[x]
= {HoldPattern[x] :> y}
= {HoldPattern[x] y}
>> y = 5;
>> OwnValues[x]
= {HoldPattern[x] :> y}
= {HoldPattern[x] y}
>> Hold[x] /. OwnValues[x]
= Hold[y]
>> Hold[x] /. OwnValues[x] // ReleaseHold
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ class Constant(Predefined):

Constant symbols cannot be used as variables in 'Solve' and
related functions:
>> Solve[x + E == 0, E]
>> Solve[x + E 0, E]
: E is not a valid variable.
= Solve[x + E == 0, E]
= Solve[x + E 0, E]
"""

summary_text = "treat as a constant in differentiation, etc"
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/drawing/drawing_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Automatic(Builtin):
graphical options:

>> Cases[Options[Plot], HoldPattern[_ :> Automatic]]
= {AxesOrigin :> Automatic, Background :> Automatic, BaselinePosition :> Automatic, ContentSelectable :> Automatic, CoordinatesToolOptions :> Automatic, Exclusions :> Automatic, FrameTicks :> Automatic, ImageSize :> Automatic, Method :> Automatic, PlotRange :> Automatic, PlotRangePadding :> Automatic, PlotRegion :> Automatic, PreserveImageOptions :> Automatic, Ticks :> Automatic}
= {AxesOrigin Automatic, Background Automatic, BaselinePosition Automatic, ContentSelectable Automatic, CoordinatesToolOptions Automatic, Exclusions Automatic, FrameTicks Automatic, ImageSize Automatic, Method Automatic, PlotRange Automatic, PlotRangePadding Automatic, PlotRegion Automatic, PreserveImageOptions Automatic, Ticks Automatic}
"""

summary_text = "graph option value to choose parameters automatically"
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/fileformats/htmlformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class DataImport(_DataImport):
</dl>

>> Import["ExampleData/PrimeMeridian.html", "Data"][[1, 1, 2, 3]]
= {Washington, D.C., 77...03′56.07″ W (1897) or 77...04′02.24″ W (NAD 27) or 77...04′01.16″ W (NAD 83), New Naval Observatory meridian}
= {Washington, D.C., 77°03′56.07″ W (1897) or 77°04′02.24″ W (NAD 27) or 77°04′01.16″ W (NAD 83), New Naval Observatory meridian}

#> Length[Import["ExampleData/PrimeMeridian.html", "Data"]]
= 3
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/fileformats/xmlformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class XMLObjectImport(Builtin):
= XMLElement[identification, {}, {XMLElement[encoding, {}, {XMLElement[software, {}, {MuseScore 1.2}], XMLElement[encoding-date, {}, {2012-09-12}]}]}]

>> Part[Import["ExampleData/Namespaces.xml"], 2]
= XMLElement[book, {{http://www.w3.org/2000/xmlns/, xmlns} -> urn:loc.gov:books}, {XMLElement[title, {}, {Cheaper by the Dozen}], XMLElement[{urn:ISBN:0-395-36341-6, number}, {}, {1568491379}], XMLElement[notes, {}, {XMLElement[p, {{http://www.w3.org/2000/xmlns/, xmlns} -> http://www.w3.org/1999/xhtml}, {This is a, XMLElement[i, {}, {funny, book!}]}]}]}]
= XMLElement[book, {{http://www.w3.org/2000/xmlns/, xmlns} urn:loc.gov:books}, {XMLElement[title, {}, {Cheaper by the Dozen}], XMLElement[{urn:ISBN:0-395-36341-6, number}, {}, {1568491379}], XMLElement[notes, {}, {XMLElement[p, {{http://www.w3.org/2000/xmlns/, xmlns} http://www.w3.org/1999/xhtml}, {This is a, XMLElement[i, {}, {funny, book!}]}]}]}]
"""

summary_text = "import elements from xml"
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/files_io/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ class FileInformation(Builtin):
This function is totally undocumented in MMA!

>> FileInformation["ExampleData/sunflowers.jpg"]
= {File -> ..., FileType -> File, ByteCount -> 142286, Date -> ...}
= {File ..., FileType File, ByteCount 142286, Date ...}
"""

rules = {
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/files_io/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ class Import(Builtin):

## JSON
>> Import["ExampleData/colors.json"]
= {colorsArray -> {{colorName -> black, rgbValue -> (0, 0, 0), hexValue -> #000000}, {colorName -> red, rgbValue -> (255, 0, 0), hexValue -> #FF0000}, {colorName -> green, rgbValue -> (0, 255, 0), hexValue -> #00FF00}, {colorName -> blue, rgbValue -> (0, 0, 255), hexValue -> #0000FF}, {colorName -> yellow, rgbValue -> (255, 255, 0), hexValue -> #FFFF00}, {colorName -> cyan, rgbValue -> (0, 255, 255), hexValue -> #00FFFF}, {colorName -> magenta, rgbValue -> (255, 0, 255), hexValue -> #FF00FF}, {colorName -> white, rgbValue -> (255, 255, 255), hexValue -> #FFFFFF}}}
= {colorsArray {{colorName black, rgbValue (0, 0, 0), hexValue #000000}, {colorName red, rgbValue (255, 0, 0), hexValue #FF0000}, {colorName green, rgbValue (0, 255, 0), hexValue #00FF00}, {colorName blue, rgbValue (0, 0, 255), hexValue #0000FF}, {colorName yellow, rgbValue (255, 255, 0), hexValue #FFFF00}, {colorName cyan, rgbValue (0, 255, 255), hexValue #00FFFF}, {colorName magenta, rgbValue (255, 0, 255), hexValue #FF00FF}, {colorName white, rgbValue (255, 255, 255), hexValue #FFFFFF}}}
"""

messages = {
Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/functional/apply_fns_to_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Map(InfixOperator):

Map $f$ onto an association:
>> Map[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>]
= <|a -> f[1], b -> f[2], c -> f[3], d -> f[4]|>
= <|a f[1], b f[2], c f[3], d f[4]|>

Include heads:
>> Map[f, a + b + c, Heads->True]
Expand Down Expand Up @@ -256,11 +256,11 @@ class MapAt(Builtin):

Map $f$ onto at the second position of an association:
>> MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, 2]
= {a -> 1, b -> f[2], c -> 3, d -> 4}
= {a 1, b f[2], c 3, d 4}

Same as above, but select the second-from-the-end position:
>> MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, -2]
= {a -> 1, b -> 2, c -> f[3], d -> 4}
= {a 1, b 2, c f[3], d 4}

"""

Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class Format(Builtin):
Use 'InputForm' if you want to get a 'Format' definition that can be used as \
Mathics3 input:

>> Format[{a->Integrate[F[x], x]}, StandardForm] //InputForm
= Format[{a -> Integrate[F[x], x]}, StandardForm]
>> Format[{a -> Integrate[F[x], x]}, StandardForm] //InputForm
= Format[{a Integrate[F[x], x]}, StandardForm]

In WMA, you might not get something that can be used as input.

Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/list/associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class Association(Builtin):
= Association

>> <|a -> x, b -> y|>
= <|a -> x, b -> y|>
= <|a x, b y|>

>> Association[{a -> x^2, b -> y}]
= <|a -> x ^ 2, b -> y|>
= <|a x ^ 2, b y|>

Associations can be nested:
>> <|a -> x, b -> y, <|a -> z, d -> t|>|>
= <|a -> z, b -> y, d -> t|>
= <|a z, b y, d t|>
"""

error_idx = 0
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/list/rearrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ class Split(Builtin):

Split based on first element
>> Split[{x -> a, x -> y, 2 -> a, z -> c, z -> a}, First[#1] === First[#2] &]
= {{x -> a, x -> y}, {2 -> a}, {z -> c, z -> a}}
= {{x a, x y}, {2 a}, {z c, z a}}
"""

rules = {
Expand Down Expand Up @@ -1410,7 +1410,7 @@ class Union(_SetOperation):

A union of two associations:
>> Union[{a -> b}, {c -> d}]
= {a -> b, c -> d}
= {a b, c d}

A union of one item is the item. Note that the list is sorted:
>> Union[{c, b, a}]
Expand Down
Loading
Loading