diff --git a/mathics/builtin/fileformats/htmlformat.py b/mathics/builtin/fileformats/htmlformat.py index 8d79b98d59..e0b250bf5a 100644 --- a/mathics/builtin/fileformats/htmlformat.py +++ b/mathics/builtin/fileformats/htmlformat.py @@ -23,7 +23,6 @@ try: import lxml.html as lhtml except ImportError: - print("lxml.html is not available...") pass diff --git a/mathics/builtin/inout.py b/mathics/builtin/inout.py index adf4b2ce6d..a7edfd67e7 100644 --- a/mathics/builtin/inout.py +++ b/mathics/builtin/inout.py @@ -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)) diff --git a/mathics/builtin/strings.py b/mathics/builtin/strings.py index 42d5495736..30b48399bb 100644 --- a/mathics/builtin/strings.py +++ b/mathics/builtin/strings.py @@ -22,6 +22,7 @@ PrefixOperator, ) from mathics.core.expression import ( + BoxError, Expression, Symbol, SymbolFailed, @@ -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) diff --git a/mathics/core/evaluation.py b/mathics/core/evaluation.py index 5f183bd712..266303095d 100644 --- a/mathics/core/evaluation.py +++ b/mathics/core/evaluation.py @@ -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", @@ -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 @@ -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 diff --git a/mathics/test.py b/mathics/test.py index 7ae575d907..c42950e6b6 100644 --- a/mathics/test.py +++ b/mathics/test.py @@ -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) @@ -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 @@ -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: @@ -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: