@@ -246,11 +246,12 @@ def get_stack(context=1):
246246 return framelist
247247
248248
249- def _stack_frames (depth = 1 ):
249+ def _stack_frames (* , skip = 0 ):
250+ skip += 1 # Skip the frame for this generator.
250251 frame = inspect .currentframe ()
251252 while frame is not None :
252- if depth > 0 :
253- depth -= 1
253+ if skip > 0 :
254+ skip -= 1
254255 else :
255256 yield frame
256257 frame = frame .f_back
@@ -279,9 +280,10 @@ def get_source_file(self, frame):
279280
280281 return value
281282
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 ):
283284 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 ):
285287 if _is_excluded_frame (frame , excluded_modules ):
286288 continue
287289
@@ -306,18 +308,33 @@ def get_stack_trace(self, *, excluded_modules=None, include_locals=False, depth=
306308 return trace
307309
308310
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+ """
310326 config = dt_settings .get_config ()
311327 if not config ["ENABLE_STACKTRACES" ]:
312328 return []
329+ skip += 1 # Skip the frame for this function.
313330 stack_trace_recorder = getattr (_local_data , "stack_trace_recorder" , None )
314331 if stack_trace_recorder is None :
315332 stack_trace_recorder = _StackTraceRecorder ()
316333 _local_data .stack_trace_recorder = stack_trace_recorder
317334 return stack_trace_recorder .get_stack_trace (
318335 excluded_modules = config ["HIDE_IN_STACKTRACES" ],
319336 include_locals = config ["ENABLE_STACKTRACES_LOCALS" ],
320- depth = depth ,
337+ skip = skip ,
321338 )
322339
323340
0 commit comments