Skip to content

Commit 8709f0f

Browse files
living180matthiask
authored andcommitted
Tweak the get_stack_trace() API
Rename the `depth` argument to `skip` and change the semantics so that now `skip=0` has the same meaning as `depth=1`.
1 parent ff552ea commit 8709f0f

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

debug_toolbar/utils.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import unittest
22

3+
from django.test import override_settings
4+
5+
import debug_toolbar.utils
36
from debug_toolbar.utils import (
47
get_name_from_obj,
58
get_stack,
9+
get_stack_trace,
610
render_stacktrace,
711
tidy_stacktrace,
812
)
@@ -55,6 +59,20 @@ def test_importlib_path_issue_1612(self):
5559

5660

5761
class StackTraceTestCase(unittest.TestCase):
62+
@override_settings(DEBUG_TOOLBAR_CONFIG={"HIDE_IN_STACKTRACES": []})
63+
def test_get_stack_trace_skip(self):
64+
stack_trace = get_stack_trace(skip=-1)
65+
self.assertTrue(len(stack_trace) > 2)
66+
self.assertEqual(stack_trace[-1][0], debug_toolbar.utils.__file__)
67+
self.assertEqual(stack_trace[-1][2], "get_stack_trace")
68+
self.assertEqual(stack_trace[-2][0], __file__)
69+
self.assertEqual(stack_trace[-2][2], "test_get_stack_trace_skip")
70+
71+
stack_trace = get_stack_trace()
72+
self.assertTrue(len(stack_trace) > 1)
73+
self.assertEqual(stack_trace[-1][0], __file__)
74+
self.assertEqual(stack_trace[-1][2], "test_get_stack_trace_skip")
75+
5876
def test_deprecated_functions(self):
5977
with self.assertWarns(DeprecationWarning):
6078
stack = get_stack()

0 commit comments

Comments
 (0)