@@ -144,9 +144,23 @@ def get_data(self):
144144 }
145145
146146
147+ class Output (object ):
148+ def max_stored_size (self , settings ):
149+ return settings .MAX_STORED_SIZE
150+
151+ def out (self , out ):
152+ pass
153+
154+ def clear (self , wait ):
155+ raise NotImplementedError
156+
157+ def display (self , data , metadata ):
158+ raise NotImplementedError
159+
160+
147161class Evaluation (object ):
148162 def __init__ (self , definitions = None ,
149- out_callback = None , format = 'text' , catch_interrupt = True ):
163+ output = None , format = 'text' , catch_interrupt = True ):
150164 from mathics .core .definitions import Definitions
151165
152166 if definitions is None :
@@ -156,7 +170,7 @@ def __init__(self, definitions=None,
156170 self .timeout = False
157171 self .stopped = False
158172 self .out = []
159- self .out_callback = out_callback
173+ self .output = output if output else Output ()
160174 self .listeners = {}
161175 self .options = None
162176 self .predetermined_out = None
@@ -221,7 +235,7 @@ def evaluate():
221235 self .definitions .add_rule ('Out' , Rule (
222236 Expression ('Out' , line_no ), stored_result ))
223237 if result != Symbol ('Null' ):
224- return self .format_output (result )
238+ return self .format_output (result , self . format )
225239 else :
226240 return None
227241 try :
@@ -261,7 +275,7 @@ def evaluate():
261275 if exc_result is not None :
262276 self .recursion_depth = 0
263277 if exc_result != Symbol ('Null' ):
264- result = self .format_output (exc_result )
278+ result = self .format_output (exc_result , self . format )
265279
266280 result = Result (self .out , result , line_no )
267281 self .out = []
@@ -288,23 +302,31 @@ def get_stored_result(self, result):
288302
289303 # Prevent too large results from being stored, as this can exceed the
290304 # DB's max_allowed_packet size
291- data = pickle .dumps (result )
292- if len (data ) > 10000 :
293- return Symbol ('Null' )
305+ max_stored_size = self .output .max_stored_size (settings )
306+ if max_stored_size is not None :
307+ data = pickle .dumps (result )
308+ if len (data ) > max_stored_size :
309+ return Symbol ('Null' )
294310 return result
295311
296312 def stop (self ):
297313 self .stopped = True
298314
299- def format_output (self , expr ):
315+ def format_output (self , expr , format = None ):
316+ if format is None :
317+ format = self .format
318+
319+ if isinstance (format , dict ):
320+ return dict ((k , self .format_output (expr , f )) for k , f in format .items ())
321+
300322 from mathics .core .expression import Expression , BoxError
301323
302- if self . format == 'text' :
324+ if format == 'text' :
303325 result = expr .format (self , 'System`OutputForm' )
304- elif self . format == 'xml' :
326+ elif format == 'xml' :
305327 result = Expression (
306328 'StandardForm' , expr ).format (self , 'System`MathMLForm' )
307- elif self . format == 'tex' :
329+ elif format == 'tex' :
308330 result = Expression ('StandardForm' , expr ).format (
309331 self , 'System`TeXForm' )
310332 else :
@@ -368,20 +390,18 @@ def message(self, symbol, tag, *args):
368390 text = String ("Message %s::%s not found." % (symbol_shortname , tag ))
369391
370392 text = self .format_output (Expression (
371- 'StringForm' , text , * (from_python (arg ) for arg in args )))
393+ 'StringForm' , text , * (from_python (arg ) for arg in args )), 'text' )
372394
373395 self .out .append (Message (symbol_shortname , tag , text ))
374- if self .out_callback :
375- self .out_callback (self .out [- 1 ])
396+ self .output .out (self .out [- 1 ])
376397
377398 def print_out (self , text ):
378399 from mathics .core .expression import from_python
379400
380- text = self .format_output (from_python (text ))
401+ text = self .format_output (from_python (text ), 'text' )
381402
382403 self .out .append (Print (text ))
383- if self .out_callback :
384- self .out_callback (self .out [- 1 ])
404+ self .output .out (self .out [- 1 ])
385405 if settings .DEBUG_PRINT :
386406 print ('OUT: ' + text )
387407
0 commit comments