diff --git a/mathics/builtin/assignment.py b/mathics/builtin/assignment.py index b6af29319..cc5ea64e4 100644 --- a/mathics/builtin/assignment.py +++ b/mathics/builtin/assignment.py @@ -315,9 +315,8 @@ def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False): # $Context = $Context <> "test`" # if new_context.startswith("`"): - new_context = ( - evaluation.definitions.get_current_context() - + new_context.lstrip("`") + new_context = evaluation.definitions.get_current_context() + new_context.lstrip( + "`" ) evaluation.definitions.set_current_context(new_context) @@ -1320,7 +1319,7 @@ def do_clear(self, definition): def apply_all(self, evaluation): "ClearAll[System`All]" evaluation.cache_result = False - evaluation.cache_expr = {} + evaluation.cache_expr = {} evaluation.definitions.set_user_definitions({}) evaluation.definitions.clear_pymathics_modules() return @@ -1929,7 +1928,7 @@ class LoadModule(Builtin): def apply(self, module, evaluation): "LoadModule[module_String]" - evaluation.cache_result = False + evaluation.cache_result = False try: evaluation.definitions.load_pymathics_module(module.value) except PyMathicsLoadException: diff --git a/mathics/builtin/datentime.py b/mathics/builtin/datentime.py index eb1cc4a00..0ab4bc004 100644 --- a/mathics/builtin/datentime.py +++ b/mathics/builtin/datentime.py @@ -213,8 +213,8 @@ def apply(self, expr, evaluation): "Timing[expr_]" start = time.process_time() result = expr.evaluate(evaluation) - evaluation.cache_result = False stop = time.process_time() + evaluation.cache_result = False return Expression("List", Real(stop - start), result) diff --git a/mathics/builtin/files_io/files.py b/mathics/builtin/files_io/files.py index 04c6a3f18..db4fcecde 100644 --- a/mathics/builtin/files_io/files.py +++ b/mathics/builtin/files_io/files.py @@ -2152,7 +2152,7 @@ class Put(BinaryOperator): def apply(self, exprs, filename, evaluation): "Put[exprs___, filename_String]" evaluation.cache_result = False - + instream = Expression("OpenWrite", filename).evaluate(evaluation) if len(instream.leaves) == 2: name, n = instream.leaves @@ -2165,7 +2165,7 @@ def apply(self, exprs, filename, evaluation): def apply_input(self, exprs, name, n, evaluation): "Put[exprs___, OutputStream[name_, n_]]" evaluation.cache_result = False - + stream = stream_manager.lookup_stream(n.get_int_value()) if stream is None or stream.io.closed: @@ -2186,7 +2186,7 @@ def apply_input(self, exprs, name, n, evaluation): def apply_default(self, exprs, filename, evaluation): "Put[exprs___, filename_]" evaluation.cache_result = False - + expr = Expression("Put", exprs, filename) evaluation.message("General", "stream", filename) return expr @@ -2248,7 +2248,7 @@ class PutAppend(BinaryOperator): def apply(self, exprs, filename, evaluation): "PutAppend[exprs___, filename_String]" evaluation.cache_result = False - + instream = Expression("OpenAppend", filename).evaluate(evaluation) if len(instream.leaves) == 2: name, n = instream.leaves @@ -2261,7 +2261,7 @@ def apply(self, exprs, filename, evaluation): def apply_input(self, exprs, name, n, evaluation): "PutAppend[exprs___, OutputStream[name_, n_]]" evaluation.cache_result = False - + stream = stream_manager.lookup_stream(n.get_int_value()) if stream is None or stream.io.closed: @@ -2282,7 +2282,7 @@ def apply_input(self, exprs, name, n, evaluation): def apply_default(self, exprs, filename, evaluation): "PutAppend[exprs___, filename_]" evaluation.cache_result = False - + expr = Expression("PutAppend", exprs, filename) evaluation.message("General", "stream", filename) return expr @@ -2452,7 +2452,7 @@ class FilePrint(Builtin): def apply(self, path, evaluation, options): "FilePrint[path_ OptionsPattern[FilePrint]]" evaluation.cache_result = False - + pypath = path.to_python() if not ( isinstance(pypath, str) @@ -2584,13 +2584,13 @@ def apply_input(self, name, n, evaluation): def apply_output(self, name, n, evaluation): "StreamPosition[OutputStream[name_, n_]]" evaluation.cache_result = False - + self.input_apply(name, n, evaluation) def apply_default(self, stream, evaluation): "StreamPosition[stream_]" evaluation.cache_result = False - + evaluation.message("General", "stream", stream) return @@ -2639,7 +2639,7 @@ class SetStreamPosition(Builtin): def apply_input(self, name, n, m, evaluation): "SetStreamPosition[InputStream[name_, n_], m_]" evaluation.cache_result = False - + stream = stream_manager.lookup_stream(n.get_int_value()) if stream is None or stream.io is None or stream.io.closed: @@ -2672,13 +2672,13 @@ def apply_input(self, name, n, m, evaluation): def apply_output(self, name, n, m, evaluation): "SetStreamPosition[OutputStream[name_, n_], m_]" evaluation.cache_result = False - + return self.apply_input(name, n, m, evaluation) def apply_default(self, stream, evaluation): "SetStreamPosition[stream_]" evaluation.cache_result = False - + evaluation.message("General", "stream", stream) return @@ -2854,7 +2854,7 @@ class InputStream(Builtin): def apply(self, name, n, evaluation): "InputStream[name_, n_]" evaluation.cache_result = False - + return @@ -2876,7 +2876,7 @@ class OutputStream(Builtin): def apply(self, name, n, evaluation): "OutputStream[name_, n_]" evaluation.cache_result = False - + return @@ -2905,7 +2905,7 @@ class StringToStream(Builtin): def apply(self, string, evaluation): "StringToStream[string_]" evaluation.cache_result = False - + pystring = string.to_python()[1:-1] fp = io.StringIO(str(pystring)) @@ -2941,13 +2941,13 @@ class Streams(Builtin): def apply(self, evaluation): "Streams[]" evaluation.cache_result = False - + return self.apply_name(None, evaluation) def apply_name(self, name, evaluation): "Streams[name_String]" evaluation.cache_result = False - + result = [] for stream in stream_manager.STREAMS.values(): if stream is None or stream.io.closed: diff --git a/mathics/builtin/files_io/importexport.py b/mathics/builtin/files_io/importexport.py index f34065ffe..4ac035340 100644 --- a/mathics/builtin/files_io/importexport.py +++ b/mathics/builtin/files_io/importexport.py @@ -1207,6 +1207,7 @@ def apply(self, url, elements, evaluation, options={}): import tempfile import os + evaluation.cache_result = False py_url = url.get_string_value() diff --git a/mathics/builtin/system.py b/mathics/builtin/system.py index fbfe39268..b34ff04e8 100644 --- a/mathics/builtin/system.py +++ b/mathics/builtin/system.py @@ -623,6 +623,7 @@ class ClearSystemCache(Builtin): """ + def apply_clear(self, evaluation): "ClearSystemCache[]" evaluation.cache_result = False @@ -640,4 +641,3 @@ def apply_clear_numeric(self, evaluation): evaluation.cache_result = False evaluation.definitions.cache_eval = {} return - diff --git a/mathics/core/evaluation.py b/mathics/core/evaluation.py index c25c13060..f4c046433 100644 --- a/mathics/core/evaluation.py +++ b/mathics/core/evaluation.py @@ -238,6 +238,7 @@ def __init__( ) -> None: from mathics.core.definitions import Definitions from mathics.core.expression import Symbol + if definitions is None: definitions = Definitions() self.definitions = definitions @@ -266,7 +267,6 @@ def __init__( self.cache_eval = {} self.cache_result = False - def parse(self, query): "Parse a single expression and print the messages." from mathics.core.parser import MathicsSingleLineFeeder diff --git a/mathics/core/expression.py b/mathics/core/expression.py index 2e3c8defe..01f6361cc 100644 --- a/mathics/core/expression.py +++ b/mathics/core/expression.py @@ -144,12 +144,7 @@ def from_python(arg): # return Symbol(arg) elif isinstance(arg, dict): entries = [ - Expression( - "Rule", - from_python(key), - from_python(arg[key]), - ) - for key in arg + Expression("Rule", from_python(key), from_python(arg[key]),) for key in arg ] return Expression(SymbolList, *entries) elif isinstance(arg, BaseExpression): @@ -1318,6 +1313,12 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]: # evaluation.cache_result can be set here or from inside the evaluation # of a branch. Once it is set to false, the result is not cached, # and hence, not used. + if evaluation.cache_result and ( + self.get_head_name() + in ("System`Out", "System`ToBoxes", "System`MakeBoxes",) + ): + evaluation.cache_result = False + if evaluation.cache_result: expr_hash = str(self.__hash__()) else: @@ -1894,21 +1895,17 @@ def thread(self, evaluation, head=None) -> typing.Tuple[bool, "Expression"]: return True, Expression(head, *leaves) def is_numeric(self) -> bool: - return ( - self._head.get_name() - in system_symbols( - "Sqrt", - "Times", - "Plus", - "Subtract", - "Minus", - "Power", - "Abs", - "Divide", - "Sin", - ) - and all(leaf.is_numeric() for leaf in self._leaves) - ) + return self._head.get_name() in system_symbols( + "Sqrt", + "Times", + "Plus", + "Subtract", + "Minus", + "Power", + "Abs", + "Divide", + "Sin", + ) and all(leaf.is_numeric() for leaf in self._leaves) # TODO: complete list of numeric functions, or access NumericFunction # attribute