Skip to content

Commit

Permalink
Small changes to stack printing & Tracestack[] doc
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Dec 20, 2024
1 parent 2b93fd7 commit a079570
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
28 changes: 15 additions & 13 deletions mathics/builtin/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PrintTrace(_TraceBase):
Note: before '$TraceBuiltins' is set to 'True', 'PrintTrace[]' will print an empty
list.
>> PrintTrace[]
>> PrintTrace[] (* See console log *)
>> $TraceBuiltins = True
= True
Expand Down Expand Up @@ -158,19 +158,21 @@ class Stacktrace(_TraceBase):
<dd>Print Mathics3 stack trace of evalutations leading to this point
</dl>
Show evaluation stack when computing a homegrown factorial function:
To show the Mathics3 evaluation stack at the \
point where expression $expr$ is evaluated, wrap $expr$ inside '{$expr$ Stacktrace[]}[1]]' \
or something similar.
>> F[0] := {1, Stacktrace[]}[[1]]
= None
Here is a complete example. To show the evaluation stack when computing a homegrown \
factorial function:
>> F[n_] := n * F[n-1]
>> F[0] := {1, Stacktrace[]}[[1]]; F[n_] := n * F[n-1]
= None
>> F[3]
>> F[3] (* See console log *)
= None
The actual 'Stacktrace[0]' call is hidden from the output so when \
run on its own nothing app
The actual 'Stacktrace[0]' call is hidden from the output; so when \
run on its own, nothing appears.
>> Stacktrace[]
= None
Expand All @@ -183,7 +185,7 @@ class Stacktrace(_TraceBase):
def eval(self, evaluation: Evaluation):
"Stacktrace[]"

# use longer-form resolve call
# Use longer-form resolve call
# so a debugger can change this.
mathics.eval.trace.eval_Stacktrace()
return SymbolNull
Expand All @@ -209,22 +211,22 @@ class TraceBuiltins(_TraceBase):
</ul>
>> TraceBuiltins[Graphics3D[Tetrahedron[]]]
>> TraceBuiltins[Graphics3D[Tetrahedron[]]] (* See console log *)
= -Graphics3D-
By default, the output is sorted by the name:
>> TraceBuiltins[Times[x, x]]
>> TraceBuiltins[Times[x, x]] (* See console log *)
= x ^ 2
By default, the output is sorted by the number of calls of the builtin from \
highest to lowest:
>> TraceBuiltins[Times[x, x], SortBy->"count"]
>> TraceBuiltins[Times[x, x], SortBy->"count"] (* See console log *)
= x ^ 2
You can have results ordered by name, or time.
Trace an expression and list the result by time from highest to lowest.
>> TraceBuiltins[Times[x, x], SortBy->"time"]
>> TraceBuiltins[Times[x, x], SortBy->"time"] (* See console log *)
= x ^ 2
"""

Expand Down
23 changes: 14 additions & 9 deletions mathics/eval/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from math import log10
from typing import Any, Tuple

from mathics.core.expression import Expression


def eval_Stacktrace():
"""
Display the Python call stack but filtered so that we Builtin calls.
"""
from mathics.core.expression import Expression

frame = inspect.currentframe()
assert frame is not None
Expand All @@ -33,15 +34,19 @@ def eval_Stacktrace():
last_was_eval = True
builtin_class = self_obj.__class__
mathics_builtin_name = builtin_class.__name__
if len(frame.f_code.co_consts) > 0:
# Use code's __doc__ string
frame_str = frame.f_code.co_consts[0]
if frame_str.startswith("%(name)s"):
frame_str = frame_str.replace(
"%(name)s", mathics_builtin_name
)
eval_name = frame.f_code.co_name
if hasattr(self_obj, eval_name):
docstring = getattr(self_obj, eval_name).__doc__
docstring = docstring.replace("%(name)s", mathics_builtin_name)
args_pattern = (
docstring[len(mathics_builtin_name) + 1 : -1]
if docstring.startswith(mathics_builtin_name)
else ""
)
else:
frame_str = mathics_builtin_name
args_pattern = ""

frame_str = f"{mathics_builtin_name}[{args_pattern}]"
frames.append(frame_str)
frame_number += 1
frame = frame.f_back
Expand Down

0 comments on commit a079570

Please sign in to comment.