Skip to content

Commit 1ea9dfb

Browse files
committed
Add deprecation warnings to old stack trace functions
1 parent d70257c commit 1ea9dfb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

debug_toolbar/utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import linecache
33
import os.path
44
import sys
5+
import warnings
56
from importlib import import_module
67
from pprint import pformat
78

@@ -49,6 +50,15 @@ def omit_path(path):
4950
return any(path.startswith(hidden_path) for hidden_path in hidden_paths)
5051

5152

53+
def _stack_trace_deprecation_warning():
54+
warnings.warn(
55+
"get_stack() and tidy_stacktrace() are deprecated in favor of"
56+
" get_stack_trace()",
57+
DeprecationWarning,
58+
stacklevel=2,
59+
)
60+
61+
5262
def tidy_stacktrace(stack):
5363
"""
5464
Clean up stacktrace and remove all entries that are excluded by the
@@ -57,6 +67,8 @@ def tidy_stacktrace(stack):
5767
``stack`` should be a list of frame tuples from ``inspect.stack()`` or
5868
``debug_toolbar.utils.get_stack()``.
5969
"""
70+
_stack_trace_deprecation_warning()
71+
6072
trace = []
6173
for frame, path, line_no, func_name, text in (f[:5] for f in stack):
6274
if omit_path(os.path.realpath(path)):
@@ -239,6 +251,8 @@ def get_stack(context=1):
239251
240252
Modified version of ``inspect.stack()`` which calls our own ``getframeinfo()``
241253
"""
254+
_stack_trace_deprecation_warning()
255+
242256
frame = sys._getframe(1)
243257
framelist = []
244258
while frame:

tests/test_utils.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import unittest
22

3-
from debug_toolbar.utils import get_name_from_obj, render_stacktrace
3+
from debug_toolbar.utils import (
4+
get_name_from_obj,
5+
get_stack,
6+
render_stacktrace,
7+
tidy_stacktrace,
8+
)
49

510

611
class GetNameFromObjTestCase(unittest.TestCase):
@@ -47,3 +52,13 @@ def test_importlib_path_issue_1612(self):
4752
'<span class="djdt-file">&lt;frozen importlib._bootstrap&gt;</span> in',
4853
result,
4954
)
55+
56+
57+
class StackTraceTestCase(unittest.TestCase):
58+
def test_deprecated_functions(self):
59+
with self.assertWarns(DeprecationWarning):
60+
stack = get_stack()
61+
self.assertEqual(stack[0][1], __file__)
62+
with self.assertWarns(DeprecationWarning):
63+
stack_trace = tidy_stacktrace(reversed(stack))
64+
self.assertEqual(stack_trace[-1][0], __file__)

0 commit comments

Comments
 (0)