Skip to content

Commit b142976

Browse files
committed
WIP
1 parent f477f35 commit b142976

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

mathics/core/util.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from platform import python_implementation
1010
from typing import Optional
1111

12+
from mathics.core.symbols import Symbol
13+
1214
IS_PYPY = python_implementation() == "PyPy"
1315

1416

@@ -115,14 +117,20 @@ def subranges(
115117
)
116118

117119

118-
def print_expression_tree(expr, indent=1):
119-
"""Print a Mathics Expression as an indented tree"""
120-
if not hasattr(expr, "elements"):
121-
print(" " * indent + str(expr))
120+
def print_expression_tree(expr, indent="", marker = lambda expr: ""):
121+
"""
122+
Print a Mathics Expression as an indented tree.
123+
Caller may supply a marker function that computes a marker
124+
to be displayed in the tree for the given node.
125+
"""
126+
if isinstance(expr, Symbol):
127+
print(f"{indent}{marker(expr)}{expr}")
128+
elif not hasattr(expr, "elements"):
129+
print(f"{indent}{marker(expr)}{expr.get_head()} {expr}")
122130
else:
123-
print(" " * indent + str(expr.head))
131+
print(f"{indent}{marker(expr)}{expr.head}")
124132
for elt in expr.elements:
125-
print_expression_tree(elt, indent + 1)
133+
print_expression_tree(elt, indent + " ", marker = marker)
126134

127135

128136
def print_sympy_tree(expr, indent=""):

test/builtin/drawing/test_plot.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,19 @@ def test_plot(str_expr, msgs, str_expected, fail_msg):
208208

209209
#
210210
# Call plotting functions and examine the structure of the output
211+
# In case of error trees are printed with an embedded >>> marker showing location of error
211212
#
212213

214+
def print_expression_tree_with_marker(expr):
215+
print_expression_tree(expr, marker = lambda expr: getattr(expr, "_marker", ""))
216+
217+
213218
def check_structure(result, expected):
214219
"""Check that expected is a prefix of result at every node"""
215220

216221
def error(msg):
217-
print(f"\nERROR: {msg}", file=sys.stderr)
218-
print("=== result:")
219-
print_expression_tree(result)
220-
print("=== expected:")
221-
print_expression_tree(expected)
222+
result._marker = "RESULT >>> "
223+
expected._marker = "EXPECTED >>> "
222224
raise AssertionError(msg)
223225

224226
# do the heads match?
@@ -242,8 +244,16 @@ def eval_and_check_structure(str_expr, str_expected):
242244
expr = session.parse(str_expr)
243245
result = expr.evaluate(session.evaluation)
244246
expected = session.parse(str_expected)
245-
check_structure(result, expected)
246-
247+
try:
248+
check_structure(result, expected)
249+
except AssertionError as oops:
250+
print(f"\nERROR: {oops} (error is marked with >>>)")
251+
print("=== result:")
252+
print_expression_tree_with_marker(result)
253+
print("=== expected:")
254+
print_expression_tree_with_marker(expected)
255+
raise
256+
247257

248258
def test_plot3d_default():
249259
eval_and_check_structure(
@@ -261,9 +271,9 @@ def test_plot3d_default():
261271
Polygon[{{0.0,0.0,0.0}, {0.0,0.5,0.5}, {0.5,0.0,0.5}}],
262272
Polygon[{{}}]
263273
},
264-
AspectRatio -> 1,
274+
XAspectRatio -> 1,
265275
Axes -> True,
266-
XAxesStyle -> {},
276+
AxesStyle -> {},
267277
Background -> Automatic,
268278
BoxRatios -> {1, 1, 0.4},
269279
ImageSize -> Automatic,

0 commit comments

Comments
 (0)