Skip to content
Closed
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
1 change: 0 additions & 1 deletion mathics/builtin/fileformats/htmlformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
try:
import lxml.html as lhtml
except ImportError:
print("lxml.html is not available...")
pass


Expand Down
3 changes: 2 additions & 1 deletion mathics/builtin/inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2784,7 +2784,8 @@ def apply_makeboxes(self, expr, n, f, evaluation):
try:
val = convert_base(x, base, p)
except ValueError:
return evaluation.message("BaseForm", "basf", n)
evaluation.message("BaseForm", "basf", n)
return None

if f.get_name() == "System`OutputForm":
return from_python("%s_%d" % (val, base))
Expand Down
9 changes: 9 additions & 0 deletions mathics/builtin/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PrefixOperator,
)
from mathics.core.expression import (
BoxError,
Expression,
Symbol,
SymbolFailed,
Expand Down Expand Up @@ -1887,7 +1888,15 @@ def apply_default(self, value, evaluation, options):
def apply_form(self, value, form, evaluation, options):
"ToString[value_, form_, OptionsPattern[ToString]]"
encoding = options["System`CharacterEncoding"]
if value.has_form("HoldForm", None):
if len(value._leaves) == 1:
value = value._leaves[0]
else:
value = Expression("Sequence", *(value._leaves))
text = value.format(evaluation, form.get_name(), encoding=encoding)
if text is None or text.has_form("MakeBoxes", None):
raise BoxError(value, form)

text = text.boxes_to_text(evaluation=evaluation)
return String(text)

Expand Down
39 changes: 30 additions & 9 deletions mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mathics_scanner import TranslateError

from mathics import settings
from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted, SymbolList, SymbolNull
from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted, SymbolList, SymbolNull, String, Symbol

FORMATS = [
"StandardForm",
Expand Down Expand Up @@ -231,10 +231,23 @@ def __init__(
self, definitions=None, output=None, format="text", catch_interrupt=True
) -> None:
from mathics.core.definitions import Definitions
from mathics.core.expression import Symbol

from mathics.core.expression import Symbol, String
if definitions is None:
definitions = Definitions()
# This code is for debugging, to avoid to pass
# through evaluation in format.
from mathics.builtin.strings import ToString
self.tostring = ToString(expression=False)
tostropts = {'System`CharacterEncoding': String("Unicode"),
'System`FormatType': Symbol('OutputForm'),
'System`NumberMarks': Symbol('$NumberMarks'),
'System`PageHeight': Symbol('Infinity'),
'System`PageWidth': Symbol('Infinity'),
'System`TotalHeight': Symbol('Infinity'),
'System`TotalWidth': Symbol('Infinity')
}
self.tostring.options = tostropts
#######
self.definitions = definitions
self.recursion_depth = 0
self.timeout = False
Expand Down Expand Up @@ -437,23 +450,31 @@ def format_output(self, expr, format=None):

from mathics.core.expression import Expression, BoxError

fmtsymbol = None
if format == "text":
result = expr.format(self, "System`OutputForm")
fmtsymbol = Symbol("System`OutputForm")
elif format == "xml":
result = Expression("StandardForm", expr).format(self, "System`MathMLForm")
fmtsymbol = Symbol("System`MathMLForm")
elif format == "tex":
result = Expression("StandardForm", expr).format(self, "System`TeXForm")
fmtsymbol = Symbol("System`TeXForm")
elif format == "unformatted":
self.exc_result = None
return expr
else:
raise ValueError

try:
boxes = result.boxes_to_text(evaluation=self)
hfexpr = Expression("HoldForm", expr)
# The next uncommented lines are just for debug. Eventually,
# we can go bach to the commented line...
# Expression("ToString", hfexpr, fmtsymbol).evaluate(self)
opts = self.tostring.options
result = self.tostring.apply_form(hfexpr, fmtsymbol, self, opts)
boxes = result.value if result is not None else None
except BoxError:
result = self.tostring.apply_form(hfexpr, Symbol("FullForm"), self, opts)
self.message(
"General", "notboxes", Expression("FullForm", result).evaluate(self)
"General", "notboxes",
Expression("MakeBoxes", result, fmtsymbol)
)
boxes = None
return boxes
Expand Down
39 changes: 35 additions & 4 deletions mathics/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ def max_stored_size(self, settings):
MAX_TESTS = 100000 # Number than the total number of tests


def str_to_wlstr(string, evaluation):
if not isinstance(string, str):
string = str(string)
string = string.replace(r"\:", r"\[Backslash]\[Colon]")
string = string.replace(r'"', r'\"')
string = "\""+ string + "\""
# print(" string:<<", string, ">>")
try:
result = evaluation.parse(string)
except:
result = None
if result is None:
# Maybe a more sofisticate processing is
# needed here.
return string
# print(" result: ", result)
return result.value


def print_and_log(*args):
global logfile
string = "".join(args)
Expand All @@ -46,8 +65,17 @@ def print_and_log(*args):
logfile.write(string)


def compare(result, wanted):
if result == wanted:
def compare(result, wanted, evaluation):
if result is None:
if wanted is None:
return True
else:
return False
else:
if wanted is None:
return False

if str_to_wlstr(result, evaluation) == str_to_wlstr(wanted, evaluation):
return True
if result is None or wanted is None:
return False
Expand Down Expand Up @@ -110,7 +138,7 @@ def fail(why):
return False

time_comparing = datetime.now()
comparison_result = compare(result, wanted)
comparison_result = compare(result, wanted, evaluation)
if check_partial_enlapsed_time:
print(" comparison took ", datetime.now() - time_comparing)
if not comparison_result:
Expand All @@ -126,7 +154,10 @@ def fail(why):
output_ok = False
else:
for got, wanted in zip(out, wanted_out):
if not got == wanted:
if not (
got.is_message == wanted.is_message and
str_to_wlstr(got.text, evaluation) == str_to_wlstr(got.text, evaluation)
):
output_ok = False
break
if check_partial_enlapsed_time:
Expand Down