Skip to content

Commit 21d25fe

Browse files
authored
Merge pull request #1025 from mathics/addCatchThrow
Add Catch/Throw and With
2 parents 7e9519e + 64bb368 commit 21d25fe

6 files changed

Lines changed: 22 additions & 24 deletions

File tree

mathics/builtin/files.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
Complex,
3131
String,
3232
Symbol,
33+
SymbolFalse,
3334
SymbolNull,
35+
SymbolTrue,
3436
from_python,
3537
Integer,
3638
BoxError,
@@ -4752,8 +4754,8 @@ def apply(self, filename, evaluation):
47524754
path = path_search(path)
47534755

47544756
if path is None:
4755-
return Symbol("False")
4756-
return Symbol("True")
4757+
return SymbolFalse
4758+
return SymbolTrue
47574759

47584760

47594761
class DirectoryQ(Builtin):
@@ -4795,8 +4797,8 @@ def apply(self, pathname, evaluation):
47954797
path = path_search(path)
47964798

47974799
if path is not None and osp.isdir(path):
4798-
return Symbol("True")
4799-
return Symbol("False")
4800+
return SymbolTrue
4801+
return SymbolFalse
48004802

48014803

48024804
class Needs(Builtin):

mathics/builtin/scoping.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ class With(Builtin):
6565
6666
>> n = 10
6767
= 10
68-
>> With[{n = 5}, n ^ 2]
69-
= 25
70-
>> n
71-
= 10
68+
69+
Evaluate an expression with x locally set to 5:
70+
71+
'With' works even without evaluation:
72+
>> With[{x = a}, (1 + x^2) &]
73+
= 1 + a ^ 2&
74+
75+
Use 'With' to insert values into held expressions
7276
>> With[{x=y}, Hold[x]]
7377
= Hold[y]
78+
7479
>> Table[With[{i=j}, Hold[i]],{j,1,4}]
7580
= {Hold[1], Hold[2], Hold[3], Hold[4]}
7681
>> x=5; With[{x=x}, Hold[x]]
@@ -460,6 +465,7 @@ class Contexts(Builtin):
460465
## this assignment makes sure that a definition in Global` exists
461466
>> x = 5;
462467
X> Contexts[] // InputForm
468+
463469
"""
464470

465471
def apply(self, evaluation):

test/helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from mathics.core.parser import parse, SingleLineFeeder
2+
from mathics.core.definitions import Definitions
3+
from mathics.core.evaluation import Evaluation
14
from mathics.session import MathicsSession
25

36
session = MathicsSession(add_builtin=True, catch_interrupt=False)

test/test_combinatorica.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# -*- coding: utf-8 -*-
22
from .helper import session, check_evaluation
33

4-
import sys
5-
from mathics.core.parser import parse, SingleLineFeeder
6-
from mathics.core.definitions import Definitions
7-
from mathics.core.evaluation import Evaluation
8-
import pytest
9-
104
session.evaluate(
115
"""
12-
Needs["DiscreteMath`CombinatoricaV0.9`"]
13-
"""
6+
Needs["DiscreteMath`CombinatoricaV0.9`"]
7+
"""
148
)
159

1610
# A number of examples from:

test/test_control.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from .helper import session, check_evaluation
33

4-
import sys
5-
from mathics.core.parser import parse, SingleLineFeeder
6-
from mathics.core.definitions import Definitions
7-
from mathics.core.evaluation import Evaluation
8-
import pytest
9-
10-
114
def test_catch():
125
session.evaluate(
136
"""

test/test_evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
),
4747
],
4848
)
49-
def check_evaluation(str_expr: str, str_expected: str, message=""):
49+
def test_evaluation(str_expr: str, str_expected: str, message=""):
5050
result = session.evaluate(str_expr)
5151
expected = session.evaluate(str_expected)
5252

0 commit comments

Comments
 (0)