Skip to content

Commit 1fb75ea

Browse files
authored
Merge pull request #1188 from mathics/charencodesymbols
Add $SystemCharacterEncoding and CharacterEncoding in boxes_to_text and ToString[]
2 parents 93cc41c + 956ce19 commit 1fb75ea

File tree

4 files changed

+54
-27
lines changed

4 files changed

+54
-27
lines changed

mathics/builtin/strings.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from mathics.core.expression import (Expression, Symbol, SymbolFailed, SymbolFalse, SymbolTrue, String, Integer,
1717
from_python, string_list)
1818
from mathics.builtin.lists import python_seq, convert_seq
19+
from mathics.settings import SYSTEM_CHARACTER_ENCODING
1920

2021

2122
_regex_longest = {
@@ -254,6 +255,20 @@ def unpack_bytes(codes):
254255
return unpack('B' * len(codes), codes)
255256

256257

258+
class SystemCharacterEncoding(Predefined):
259+
"""
260+
<dl>
261+
<dt>$SystemCharacterEncoding
262+
263+
</dl>
264+
"""
265+
name = "$SystemCharacterEncoding"
266+
267+
rules = {
268+
'$SystemCharacterEncoding': '"' + SYSTEM_CHARACTER_ENCODING + '"',
269+
}
270+
271+
257272
class CharacterEncoding(Predefined):
258273
"""
259274
<dl>
@@ -1576,19 +1591,19 @@ class ToString(Builtin):
15761591
= U2
15771592
"""
15781593

1579-
options = {'CharacterEncoding' : '"Unicode"',
1594+
options = { 'CharacterEncoding' : '"Unicode"',
15801595
'FormatType' : 'OutputForm',
15811596
'NumberMarks': '$NumberMarks',
15821597
'PageHeight' : 'Infinity',
15831598
'PageWidth' : 'Infinity',
15841599
'TotalHeight' : 'Infinity',
15851600
'TotalWidth' : 'Infinity'}
15861601

1587-
def apply(self, value, evaluation):
1588-
'ToString[value_]'
1589-
1590-
text = value.format(evaluation, 'System`OutputForm').boxes_to_text(
1591-
evaluation=evaluation)
1602+
def apply(self, value, evaluation, **options):
1603+
'ToString[value_, OptionsPattern[ToString]]'
1604+
encoding = options['options']["System`CharacterEncoding"]
1605+
text = value.format(evaluation, 'System`OutputForm', encoding=encoding)
1606+
text = text.boxes_to_text(evaluation=evaluation)
15921607
return String(text)
15931608

15941609

mathics/core/evaluation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mathics_scanner import TranslateError
1414

1515
from mathics import settings
16-
from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted, SymbolList, SymbolNull
16+
from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted
1717

1818
FORMATS = [
1919
"StandardForm",
@@ -250,7 +250,7 @@ def __init__(
250250
self.format = format
251251
self.catch_interrupt = catch_interrupt
252252

253-
self.SymbolNull = SymbolNull
253+
self.SymbolNull = Symbol("Null")
254254

255255
# status of last evaluate
256256
self.exc_result = self.SymbolNull
@@ -458,7 +458,7 @@ def format_output(self, expr, format=None):
458458
def set_quiet_messages(self, messages) -> None:
459459
from mathics.core.expression import Expression
460460

461-
value = Expression(SymbolList, *messages)
461+
value = Expression("List", *messages)
462462
self.definitions.set_ownvalue("Internal`$QuietMessages", value)
463463

464464
def get_quiet_messages(self):

mathics/core/expression.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ def from_python(arg):
126126
# return Symbol(arg)
127127
elif isinstance(arg, dict):
128128
entries = [
129-
Expression("Rule", from_python(key), from_python(arg[key]),) for key in arg
129+
Expression(
130+
"Rule",
131+
from_python(key),
132+
from_python(arg[key]),
133+
)
134+
for key in arg
130135
]
131136
return Expression(SymbolList, *entries)
132137
elif isinstance(arg, BaseExpression):
@@ -485,7 +490,9 @@ def format_expr(expr):
485490
finally:
486491
evaluation.dec_recursion_depth()
487492

488-
def format(self, evaluation, form) -> typing.Union["Expression", "Symbol"]:
493+
def format(
494+
self, evaluation, form, **kwargs
495+
) -> typing.Union["Expression", "Symbol"]:
489496
"""
490497
Applies formats associated to the expression, and then calls Makeboxes
491498
"""
@@ -507,7 +514,7 @@ def get_precision(self):
507514
def get_option_values(self, evaluation, allow_symbols=False, stop_on_error=True):
508515
options = self
509516
if options.has_form("List", None):
510-
options = options.flatten(Symbol("List"))
517+
options = options.flatten(SymbolList)
511518
values = options.leaves
512519
else:
513520
values = [options]
@@ -1622,9 +1629,9 @@ def filter_leaves(self, head_name):
16221629

16231630
def apply_rules(self, rules, evaluation, level=0, options=None):
16241631
"""for rule in rules:
1625-
result = rule.apply(self, evaluation, fully=False)
1626-
if result is not None:
1627-
return result"""
1632+
result = rule.apply(self, evaluation, fully=False)
1633+
if result is not None:
1634+
return result"""
16281635

16291636
# to be able to access it inside inner function
16301637
new_applied = [False]
@@ -1774,17 +1781,21 @@ def thread(self, evaluation, head=None) -> typing.Tuple[bool, "Expression"]:
17741781
return True, Expression(head, *leaves)
17751782

17761783
def is_numeric(self) -> bool:
1777-
return self._head.get_name() in system_symbols(
1778-
"Sqrt",
1779-
"Times",
1780-
"Plus",
1781-
"Subtract",
1782-
"Minus",
1783-
"Power",
1784-
"Abs",
1785-
"Divide",
1786-
"Sin",
1787-
) and all(leaf.is_numeric() for leaf in self._leaves)
1784+
return (
1785+
self._head.get_name()
1786+
in system_symbols(
1787+
"Sqrt",
1788+
"Times",
1789+
"Plus",
1790+
"Subtract",
1791+
"Minus",
1792+
"Power",
1793+
"Abs",
1794+
"Divide",
1795+
"Sin",
1796+
)
1797+
and all(leaf.is_numeric() for leaf in self._leaves)
1798+
)
17881799
# TODO: complete list of numeric functions, or access NumericFunction
17891800
# attribute
17901801

@@ -2054,7 +2065,7 @@ def is_numeric(self) -> bool:
20542065
def _ExponentFunction(value):
20552066
n = value.get_int_value()
20562067
if -5 <= n <= 5:
2057-
return Symbol("Null")
2068+
return SymbolNull
20582069
else:
20592070
return value
20602071

mathics/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@
7272
# whatever it is that setting this thing did.
7373
default_pymathics_modules = []
7474

75+
SYSTEM_CHARACTER_ENCODING = "UTF-8" if sys.getdefaultencoding()=='utf-8' else 'ASCII'

0 commit comments

Comments
 (0)