Skip to content

Commit 793279a

Browse files
committed
fix BaseForm
1 parent 476d678 commit 793279a

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

mathics/builtin/inout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,8 @@ def apply_makeboxes(self, expr, n, f, evaluation):
27842784
try:
27852785
val = convert_base(x, base, p)
27862786
except ValueError:
2787-
return evaluation.message("BaseForm", "basf", n)
2787+
evaluation.message("BaseForm", "basf", n)
2788+
return None
27882789

27892790
if f.get_name() == "System`OutputForm":
27902791
return from_python("%s_%d" % (val, base))

mathics/builtin/strings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
PrefixOperator,
2323
)
2424
from mathics.core.expression import (
25+
BoxError,
2526
Expression,
2627
Symbol,
2728
SymbolFailed,
@@ -1887,7 +1888,15 @@ def apply_default(self, value, evaluation, options):
18871888
def apply_form(self, value, form, evaluation, options):
18881889
"ToString[value_, form_, OptionsPattern[ToString]]"
18891890
encoding = options["System`CharacterEncoding"]
1891+
if value.has_form("HoldForm", None):
1892+
if len(value._leaves) == 1:
1893+
value = value._leaves[0]
1894+
else:
1895+
value = Expression("Sequence", *(value._leaves))
18901896
text = value.format(evaluation, form.get_name(), encoding=encoding)
1897+
if text is None or text.has_form("MakeBoxes", None):
1898+
raise BoxError(value, form)
1899+
18911900
text = text.boxes_to_text(evaluation=evaluation)
18921901
return String(text)
18931902

mathics/core/evaluation.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,23 @@ def __init__(
231231
self, definitions=None, output=None, format="text", catch_interrupt=True
232232
) -> None:
233233
from mathics.core.definitions import Definitions
234-
from mathics.core.expression import Symbol
235-
234+
from mathics.core.expression import Symbol, String
236235
if definitions is None:
237236
definitions = Definitions()
237+
# This code is for debugging, to avoid to pass
238+
# through evaluation in format.
239+
from mathics.builtin.strings import ToString
240+
self.tostring = ToString(expression=False)
241+
tostropts = {'System`CharacterEncoding': String("Unicode"),
242+
'System`FormatType': Symbol('OutputForm'),
243+
'System`NumberMarks': Symbol('$NumberMarks'),
244+
'System`PageHeight': Symbol('Infinity'),
245+
'System`PageWidth': Symbol('Infinity'),
246+
'System`TotalHeight': Symbol('Infinity'),
247+
'System`TotalWidth': Symbol('Infinity')
248+
}
249+
self.tostring.options = tostropts
250+
#######
238251
self.definitions = definitions
239252
self.recursion_depth = 0
240253
self.timeout = False
@@ -437,25 +450,31 @@ def format_output(self, expr, format=None):
437450

438451
from mathics.core.expression import Expression, BoxError
439452

440-
fmtstring = ""
453+
fmtsymbol = None
441454
if format == "text":
442-
fmtstring = Symbol("System`OutputForm")
455+
fmtsymbol = Symbol("System`OutputForm")
443456
elif format == "xml":
444-
fmtstring = Symbol("System`MathMLForm")
457+
fmtsymbol = Symbol("System`MathMLForm")
445458
elif format == "tex":
446-
fmtstring = Symbol("System`TeXForm")
459+
fmtsymbol = Symbol("System`TeXForm")
447460
elif format == "unformatted":
448461
self.exc_result = None
449462
return expr
450463
else:
451464
raise ValueError
452465
try:
453-
expr = Expression("HoldForm", expr)
454-
result = Expression("ToString", expr, fmtstring).evaluate(self)
455-
boxes = result.value
466+
hfexpr = Expression("HoldForm", expr)
467+
# The next uncommented lines are just for debug. Eventually,
468+
# we can go bach to the commented line...
469+
# Expression("ToString", hfexpr, fmtsymbol).evaluate(self)
470+
opts = self.tostring.options
471+
result = self.tostring.apply_form(hfexpr, fmtsymbol, self, opts)
472+
boxes = result.value if result is not None else None
456473
except BoxError:
474+
result = self.tostring.apply_form(hfexpr, Symbol("FullForm"), self, opts)
457475
self.message(
458-
"General", "notboxes", Expression("FullForm", result).evaluate(self)
476+
"General", "notboxes",
477+
Expression("MakeBoxes", result, fmtsymbol)
459478
)
460479
boxes = None
461480
return boxes

0 commit comments

Comments
 (0)