Skip to content

Commit 969a232

Browse files
committed
improve implementation of pythonized code. Fix test case
1 parent 36837e4 commit 969a232

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

mathics/builtin/compilation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ class Compile(Builtin):
6565
>> cf[3.5, 2]
6666
= 2.18888
6767
68-
Loops and variable assignments are supported usinv Python builtin "compile" function:
68+
Loops and variable assignments are supported using Python builtin "compile" function:
6969
>> Compile[{{a, _Integer}, {b, _Integer}}, While[b != 0, {a, b} = {b, Mod[a, b]}]; a] (* GCD of a, b *)
70-
= CompiledFunction[{a, b}, a, -PythonizedCode-]
70+
= CompiledFunction[{a, b}, While[b != 0, {a, b} = {b, Mod[a, b]}] ; a, -PythonizedCode-]
7171
"""
7272

7373
attributes = A_HOLD_ALL | A_PROTECTED

mathics/core/convert/function.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def evaluate_without_side_effects(
6969
# Restore the definitions
7070
for name, defin in SIDE_EFFECT_BUILTINS.items():
7171
definitions.builtin[name] = defin
72+
definitions.clear_cache(name)
7273
return result if result is not None else expr
7374

7475

@@ -86,6 +87,7 @@ def expression_to_callable(
8687
"""
8788
if evaluation is not None:
8889
expr = evaluate_without_side_effects(expr, evaluation)
90+
8991
try:
9092
cfunc = _compile(expr, args) if (use_llvm and args is not None) else None
9193
except CompileError:
@@ -97,11 +99,11 @@ def expression_to_callable(
9799
try:
98100

99101
def _pythonized_mathics_expr(*x):
100-
inner_evaluation = Evaluation(definitions=evaluation.definitions)
101-
x_mathics = (from_python(u) for u in x[: len(args)])
102-
vars = dict(list(zip([a.name for a in args], x_mathics)))
103-
pyexpr = expr.replace_vars(vars)
104-
pyexpr = eval_N(pyexpr, inner_evaluation)
102+
from mathics.eval.scoping import dynamic_scoping
103+
104+
vars = {a.name: from_python(u) for a, u in zip(args, x[: len(args)])}
105+
pyexpr = dynamic_scoping(lambda ev: expr.evaluate(ev), vars, evaluation)
106+
pyexpr = eval_N(pyexpr, evaluation)
105107
res = pyexpr.to_python()
106108
return res
107109

0 commit comments

Comments
 (0)