Skip to content

Commit

Permalink
do not catch Out and MakeBoxes
Browse files Browse the repository at this point in the history
blacken
  • Loading branch information
mmatera committed Jun 29, 2021
1 parent 74ae499 commit de14b91
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
9 changes: 4 additions & 5 deletions mathics/builtin/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/datentime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
34 changes: 17 additions & 17 deletions mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -2854,7 +2854,7 @@ class InputStream(Builtin):
def apply(self, name, n, evaluation):
"InputStream[name_, n_]"
evaluation.cache_result = False

return


Expand All @@ -2876,7 +2876,7 @@ class OutputStream(Builtin):
def apply(self, name, n, evaluation):
"OutputStream[name_, n_]"
evaluation.cache_result = False

return


Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions mathics/builtin/files_io/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class ClearSystemCache(Builtin):
</dl>
"""

def apply_clear(self, evaluation):
"ClearSystemCache[]"
evaluation.cache_result = False
Expand All @@ -640,4 +641,3 @@ def apply_clear_numeric(self, evaluation):
evaluation.cache_result = False
evaluation.definitions.cache_eval = {}
return

1 change: 1 addition & 0 deletions mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,13 @@ 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:
Expand Down

0 comments on commit de14b91

Please sign in to comment.