@@ -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