22
22
from time import time
23
23
from typing import Callable
24
24
25
+ import mathics .eval .trace
25
26
import mathics .eval .tracing
26
27
from mathics .core .attributes import A_HOLD_ALL , A_HOLD_ALL_COMPLETE , A_PROTECTED
27
28
from mathics .core .builtin import Builtin
@@ -124,7 +125,7 @@ class PrintTrace(_TraceBase):
124
125
125
126
Note: before '$TraceBuiltins' is set to 'True', 'PrintTrace[]' will print an empty
126
127
list.
127
- >> PrintTrace[]
128
+ >> PrintTrace[] (* See console log *)
128
129
129
130
>> $TraceBuiltins = True
130
131
= True
@@ -148,6 +149,46 @@ def eval(self, evaluation, options={}):
148
149
return SymbolNull
149
150
150
151
152
+ class Stacktrace (_TraceBase ):
153
+ """
154
+ ## <url>:trace native symbol:</url>
155
+
156
+ <dl>
157
+ <dt>'Stacktrace[]'
158
+ <dd>Print Mathics3 stack trace of evalutations leading to this point
159
+ </dl>
160
+
161
+ To show the Mathics3 evaluation stack at the \
162
+ point where expression $expr$ is evaluated, wrap $expr$ inside '{$expr$ Stacktrace[]}[1]]' \
163
+ or something similar.
164
+
165
+ Here is a complete example. To show the evaluation stack when computing a homegrown \
166
+ factorial function:
167
+
168
+ >> F[0] := {1, Stacktrace[]}[[1]]; F[n_] := n * F[n-1]
169
+
170
+ >> F[3] (* See console log *)
171
+ = 6
172
+
173
+ The actual 'Stacktrace[0]' call is hidden from the output; so when \
174
+ run on its own, nothing appears.
175
+
176
+ >> Stacktrace[]
177
+
178
+ #> Clear[F]
179
+ """
180
+
181
+ summary_text = "print Mathics3 function stacktrace"
182
+
183
+ def eval (self , evaluation : Evaluation ):
184
+ "Stacktrace[]"
185
+
186
+ # Use longer-form resolve call
187
+ # so a debugger can change this.
188
+ mathics .eval .trace .eval_Stacktrace ()
189
+ return SymbolNull
190
+
191
+
151
192
class TraceBuiltins (_TraceBase ):
152
193
"""
153
194
## <url>:trace native symbol:</url>
@@ -168,22 +209,22 @@ class TraceBuiltins(_TraceBase):
168
209
</ul>
169
210
170
211
171
- >> TraceBuiltins[Graphics3D[Tetrahedron[]]]
212
+ >> TraceBuiltins[Graphics3D[Tetrahedron[]]] (* See console log *)
172
213
= -Graphics3D-
173
214
174
215
By default, the output is sorted by the name:
175
- >> TraceBuiltins[Times[x, x]]
216
+ >> TraceBuiltins[Times[x, x]] (* See console log *)
176
217
= x ^ 2
177
218
178
219
By default, the output is sorted by the number of calls of the builtin from \
179
220
highest to lowest:
180
- >> TraceBuiltins[Times[x, x], SortBy->"count"]
221
+ >> TraceBuiltins[Times[x, x], SortBy->"count"] (* See console log *)
181
222
= x ^ 2
182
223
183
224
You can have results ordered by name, or time.
184
225
185
226
Trace an expression and list the result by time from highest to lowest.
186
- >> TraceBuiltins[Times[x, x], SortBy->"time"]
227
+ >> TraceBuiltins[Times[x, x], SortBy->"time"] (* See console log *)
187
228
= x ^ 2
188
229
"""
189
230
0 commit comments