Skip to content

Commit 37ad64d

Browse files
committed
mark symbols that shouldn't be cached
1 parent 51b4569 commit 37ad64d

12 files changed

Lines changed: 160 additions & 38 deletions

File tree

mathics/builtin/assignment.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class _SetOperator(object):
6565
def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False):
6666
# TODO: This function should be splitted and simplified
6767

68+
evaluation.cache_result = False
6869
name = lhs.get_head_name()
6970
lhs._format_cache = None
7071
condition = None
@@ -547,7 +548,7 @@ class Set(BinaryOperator, _SetOperator):
547548

548549
def apply(self, lhs, rhs, evaluation):
549550
"lhs_ = rhs_"
550-
551+
evaluation.cache_result = False
551552
self.assign(lhs, rhs, evaluation)
552553
return rhs
553554

@@ -600,7 +601,7 @@ class SetDelayed(Set):
600601

601602
def apply(self, lhs, rhs, evaluation):
602603
"lhs_ := rhs_"
603-
604+
evaluation.cache_result = False
604605
if self.assign(lhs, rhs, evaluation):
605606
return Symbol("Null")
606607
else:
@@ -648,7 +649,7 @@ class UpSet(BinaryOperator, _SetOperator):
648649

649650
def apply(self, lhs, rhs, evaluation):
650651
"lhs_ ^= rhs_"
651-
652+
evaluation.cache_result = False
652653
self.assign_elementary(lhs, rhs, evaluation, upset=True)
653654
return rhs
654655

@@ -681,7 +682,7 @@ class UpSetDelayed(UpSet):
681682

682683
def apply(self, lhs, rhs, evaluation):
683684
"lhs_ ^:= rhs_"
684-
685+
evaluation.cache_result = False
685686
if self.assign_elementary(lhs, rhs, evaluation, upset=True):
686687
return Symbol("Null")
687688
else:
@@ -723,7 +724,7 @@ class TagSet(Builtin, _SetOperator):
723724

724725
def apply(self, f, lhs, rhs, evaluation):
725726
"f_ /: lhs_ = rhs_"
726-
727+
evaluation.cache_result = False
727728
name = f.get_name()
728729
if not name:
729730
evaluation.message(self.get_name(), "sym", f, 1)
@@ -747,7 +748,7 @@ class TagSetDelayed(TagSet):
747748

748749
def apply(self, f, lhs, rhs, evaluation):
749750
"f_ /: lhs_ := rhs_"
750-
751+
evaluation.cache_result = False
751752
name = f.get_name()
752753
if not name:
753754
evaluation.message(self.get_name(), "sym", f, 1)
@@ -1240,6 +1241,7 @@ def do_clear(self, definition):
12401241

12411242
def apply(self, symbols, evaluation):
12421243
"%(name)s[symbols___]"
1244+
evaluation.cache_result = False
12431245
if isinstance(symbols, Symbol):
12441246
symbols = [symbols]
12451247
elif isinstance(symbols, Expression):
@@ -1276,6 +1278,8 @@ def apply(self, symbols, evaluation):
12761278

12771279
def apply_all(self, evaluation):
12781280
"Clear[System`All]"
1281+
evaluation.cache_result = False
1282+
evaluation.cache_expr = {}
12791283
evaluation.definitions.set_user_definitions({})
12801284
evaluation.definitions.clear_pymathics_modules()
12811285
return
@@ -1315,6 +1319,8 @@ def do_clear(self, definition):
13151319

13161320
def apply_all(self, evaluation):
13171321
"ClearAll[System`All]"
1322+
evaluation.cache_result = False
1323+
evaluation.cache_expr = {}
13181324
evaluation.definitions.set_user_definitions({})
13191325
evaluation.definitions.clear_pymathics_modules()
13201326
return
@@ -1403,6 +1409,7 @@ class Unset(PostfixOperator):
14031409

14041410
def apply(self, expr, evaluation):
14051411
"Unset[expr_]"
1412+
evaluation.cache_result = False
14061413

14071414
name = expr.get_head_name()
14081415
if name in system_symbols(
@@ -1659,7 +1666,7 @@ class Messages(Builtin):
16591666

16601667
def apply(self, symbol, evaluation):
16611668
"Messages[symbol_]"
1662-
1669+
evaluation.cache_result = False
16631670
return get_symbol_values(symbol, "Messages", "messages", evaluation)
16641671

16651672

@@ -1922,6 +1929,7 @@ class LoadModule(Builtin):
19221929

19231930
def apply(self, module, evaluation):
19241931
"LoadModule[module_String]"
1932+
evaluation.cache_result = False
19251933
try:
19261934
evaluation.definitions.load_pymathics_module(module.value)
19271935
except PyMathicsLoadException:

mathics/builtin/control.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class For(Builtin):
337337

338338
def apply(self, start, test, incr, body, evaluation):
339339
"For[start_, test_, incr_, body_]"
340+
evaluation.cache_result = False
340341
while test.evaluate(evaluation).is_true():
341342
evaluation.check_stopped()
342343
try:
@@ -382,7 +383,7 @@ class While(Builtin):
382383

383384
def apply(self, test, body, evaluation):
384385
"While[test_, body_]"
385-
386+
evaluation.cache_result = False
386387
while test.evaluate(evaluation).is_true():
387388
try:
388389
evaluation.check_stopped()
@@ -645,7 +646,7 @@ class Abort(Builtin):
645646

646647
def apply(self, evaluation):
647648
"Abort[]"
648-
649+
evaluation.cache_result = False
649650
raise AbortInterrupt
650651

651652

@@ -662,7 +663,7 @@ class Interrupt(Builtin):
662663

663664
def apply(self, evaluation):
664665
"Interrupt[]"
665-
666+
evaluation.cache_result = False
666667
raise AbortInterrupt
667668

668669

@@ -707,7 +708,7 @@ class Return(Builtin):
707708

708709
def apply(self, expr, evaluation):
709710
"Return[expr_]"
710-
711+
evaluation.cache_result = False
711712
raise ReturnInterrupt(expr)
712713

713714

@@ -729,7 +730,7 @@ class Break(Builtin):
729730

730731
def apply(self, evaluation):
731732
"Break[]"
732-
733+
evaluation.cache_result = False
733734
raise BreakInterrupt
734735

735736

@@ -753,7 +754,7 @@ class Continue(Builtin):
753754

754755
def apply(self, evaluation):
755756
"Continue[]"
756-
757+
evaluation.cache_result = False
757758
raise ContinueInterrupt
758759

759760

@@ -790,6 +791,7 @@ class Catch(Builtin):
790791

791792
def apply1(self, expr, evaluation):
792793
"Catch[expr_]"
794+
evaluation.cache_result = False
793795
try:
794796
ret = expr.evaluate(evaluation)
795797
except WLThrowInterrupt as e:
@@ -798,6 +800,7 @@ def apply1(self, expr, evaluation):
798800

799801
def apply3(self, expr, form, f, evaluation):
800802
"Catch[expr_, form_, f__:Identity]"
803+
evaluation.cache_result = False
801804
try:
802805
ret = expr.evaluate(evaluation)
803806
except WLThrowInterrupt as e:
@@ -840,8 +843,10 @@ class Throw(Builtin):
840843

841844
def apply1(self, value, evaluation):
842845
"Throw[value_]"
846+
evaluation.cache_result = False
843847
raise WLThrowInterrupt(value)
844848

845849
def apply_with_tag(self, value, tag, evaluation):
846850
"Throw[value_, tag_]"
851+
evaluation.cache_result = False
847852
raise WLThrowInterrupt(value, tag)

mathics/builtin/datentime.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class TimeRemaining(Builtin):
121121

122122
def apply(self, evaluation):
123123
"TimeRemaining[]"
124+
evaluation.cache_result = False
124125
if len(evaluation.timeout_queue) > 0:
125126
t, start_time = evaluation.timeout_queue[-1]
126127
curr_time = datetime.now().timestamp()
@@ -167,10 +168,12 @@ class TimeConstrained(Builtin):
167168

168169
def apply_2(self, expr, t, evaluation):
169170
"TimeConstrained[expr_, t_]"
171+
evaluation.cache_result = False
170172
return self.apply_3(expr, t, SymbolAborted, evaluation)
171173

172174
def apply_3(self, expr, t, failexpr, evaluation):
173175
"TimeConstrained[expr_, t_, failexpr_]"
176+
evaluation.cache_result = False
174177
t = t.evaluate(evaluation)
175178
if not t.is_numeric():
176179
evaluation.message("TimeConstrained", "timc", t)
@@ -208,7 +211,7 @@ class Timing(Builtin):
208211

209212
def apply(self, expr, evaluation):
210213
"Timing[expr_]"
211-
214+
evaluation.cache_result = False
212215
start = time.process_time()
213216
result = expr.evaluate(evaluation)
214217
stop = time.process_time()
@@ -232,7 +235,7 @@ class AbsoluteTiming(Builtin):
232235

233236
def apply(self, expr, evaluation):
234237
"AbsoluteTiming[expr_]"
235-
238+
evaluation.cache_result = False
236239
start = time.time()
237240
result = expr.evaluate(evaluation)
238241
stop = time.time()
@@ -498,7 +501,6 @@ class DateList(_DateFormat):
498501
def apply(self, epochtime, evaluation):
499502
"%(name)s[epochtime_]"
500503
datelist = self.to_datelist(epochtime, evaluation)
501-
502504
if datelist is None:
503505
return
504506

@@ -754,7 +756,7 @@ class AbsoluteTime(_DateFormat):
754756

755757
def apply_now(self, evaluation):
756758
"AbsoluteTime[]"
757-
759+
evaluation.cache_result = False
758760
return from_python(total_seconds(datetime.now() - EPOCH_START))
759761

760762
def apply_spec(self, epochtime, evaluation):
@@ -849,6 +851,7 @@ def apply(self, evaluation):
849851
"TimeUsed[]"
850852
# time.process_time() is better than
851853
# time.clock(). See https://bugs.python.org/issue31803
854+
evaluation.cache_result = False
852855
return Real(time.process_time())
853856

854857

@@ -865,6 +868,7 @@ class SessionTime(Builtin):
865868

866869
def apply(self, evaluation):
867870
"SessionTime[]"
871+
evaluation.cache_result = False
868872
return Real(time.time() - START_TIME)
869873

870874

@@ -886,11 +890,11 @@ class Pause(Builtin):
886890

887891
def apply(self, n, evaluation):
888892
"Pause[n_]"
893+
evaluation.cache_result = False
889894
sleeptime = n.to_python()
890895
if not isinstance(sleeptime, (int, float)) or sleeptime < 0:
891896
evaluation.message("Pause", "numnm", Expression("Pause", n))
892897
return
893-
894898
time.sleep(sleeptime)
895899
return Symbol("Null")
896900

mathics/builtin/evaluation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class RecursionLimit(Predefined):
7878
}
7979

8080
def evaluate(self, evaluation) -> Integer:
81+
evaluation.cache_result = False
8182
return Integer(self.value)
8283

8384

@@ -138,6 +139,7 @@ class IterationLimit(Predefined):
138139
}
139140

140141
def evaluate(self, evaluation):
142+
evaluation.cache_result = False
141143
return Integer(self.value)
142144

143145

0 commit comments

Comments
 (0)