@@ -246,11 +246,12 @@ def get_stack(context=1):
246
246
return framelist
247
247
248
248
249
- def _stack_frames (depth = 1 ):
249
+ def _stack_frames (* , skip = 0 ):
250
+ skip += 1 # Skip the frame for this generator.
250
251
frame = inspect .currentframe ()
251
252
while frame is not None :
252
- if depth > 0 :
253
- depth -= 1
253
+ if skip > 0 :
254
+ skip -= 1
254
255
else :
255
256
yield frame
256
257
frame = frame .f_back
@@ -279,9 +280,10 @@ def get_source_file(self, frame):
279
280
280
281
return value
281
282
282
- def get_stack_trace (self , * , excluded_modules = None , include_locals = False , depth = 1 ):
283
+ def get_stack_trace (self , * , excluded_modules = None , include_locals = False , skip = 0 ):
283
284
trace = []
284
- for frame in _stack_frames (depth = depth + 1 ):
285
+ skip += 1 # Skip the frame for this method.
286
+ for frame in _stack_frames (skip = skip ):
285
287
if _is_excluded_frame (frame , excluded_modules ):
286
288
continue
287
289
@@ -306,18 +308,33 @@ def get_stack_trace(self, *, excluded_modules=None, include_locals=False, depth=
306
308
return trace
307
309
308
310
309
- def get_stack_trace (* , depth = 1 ):
311
+ def get_stack_trace (* , skip = 0 ):
312
+ """
313
+ Return a processed stack trace for the current call stack.
314
+
315
+ If the ``ENABLE_STACKTRACES`` setting is False, return an empty :class:`list`.
316
+ Otherwise return a :class:`list` of processed stack frame tuples (file name, line
317
+ number, function name, source line, frame locals) for the current call stack. The
318
+ first entry in the list will be for the bottom of the stack and the last entry will
319
+ be for the top of the stack.
320
+
321
+ ``skip`` is an :class:`int` indicating the number of stack frames above the frame
322
+ for this function to omit from the stack trace. The default value of ``0`` means
323
+ that the entry for the caller of this function will be the last entry in the
324
+ returned stack trace.
325
+ """
310
326
config = dt_settings .get_config ()
311
327
if not config ["ENABLE_STACKTRACES" ]:
312
328
return []
329
+ skip += 1 # Skip the frame for this function.
313
330
stack_trace_recorder = getattr (_local_data , "stack_trace_recorder" , None )
314
331
if stack_trace_recorder is None :
315
332
stack_trace_recorder = _StackTraceRecorder ()
316
333
_local_data .stack_trace_recorder = stack_trace_recorder
317
334
return stack_trace_recorder .get_stack_trace (
318
335
excluded_modules = config ["HIDE_IN_STACKTRACES" ],
319
336
include_locals = config ["ENABLE_STACKTRACES_LOCALS" ],
320
- depth = depth ,
337
+ skip = skip ,
321
338
)
322
339
323
340
0 commit comments