Skip to content

Commit 2cd4a02

Browse files
authored
Merge pull request #1635 from living180/frame_skipping
Reimplement HIDE_IN_STACKTRACES machinery
2 parents c5d95ff + 56f397c commit 2cd4a02

File tree

1 file changed

+21
-48
lines changed

1 file changed

+21
-48
lines changed

debug_toolbar/utils.py

+21-48
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import os.path
44
import sys
55
import warnings
6-
from importlib import import_module
76
from pprint import pformat
87

9-
import django
108
from asgiref.local import Local
11-
from django.core.exceptions import ImproperlyConfigured
129
from django.template import Node
1310
from django.utils.html import format_html
1411
from django.utils.safestring import mark_safe
@@ -24,30 +21,17 @@
2421
_local_data = Local()
2522

2623

27-
# Figure out some paths
28-
django_path = os.path.realpath(os.path.dirname(django.__file__))
29-
30-
31-
def get_module_path(module_name):
32-
try:
33-
module = import_module(module_name)
34-
except ImportError as e:
35-
raise ImproperlyConfigured(f"Error importing HIDE_IN_STACKTRACES: {e}")
36-
else:
37-
source_path = inspect.getsourcefile(module)
38-
if source_path.endswith("__init__.py"):
39-
source_path = os.path.dirname(source_path)
40-
return os.path.realpath(source_path)
41-
42-
43-
hidden_paths = [
44-
get_module_path(module_name)
45-
for module_name in dt_settings.get_config()["HIDE_IN_STACKTRACES"]
46-
]
47-
48-
49-
def omit_path(path):
50-
return any(path.startswith(hidden_path) for hidden_path in hidden_paths)
24+
def _is_excluded_frame(frame, excluded_modules):
25+
if not excluded_modules:
26+
return False
27+
frame_module = frame.f_globals.get("__name__")
28+
if not isinstance(frame_module, str):
29+
return False
30+
return any(
31+
frame_module == excluded_module
32+
or frame_module.startswith(excluded_module + ".")
33+
for excluded_module in excluded_modules
34+
)
5135

5236

5337
def _stack_trace_deprecation_warning():
@@ -70,8 +54,9 @@ def tidy_stacktrace(stack):
7054
_stack_trace_deprecation_warning()
7155

7256
trace = []
57+
excluded_modules = dt_settings.get_config()["HIDE_IN_STACKTRACES"]
7358
for frame, path, line_no, func_name, text in (f[:5] for f in stack):
74-
if omit_path(os.path.realpath(path)):
59+
if _is_excluded_frame(frame, excluded_modules):
7560
continue
7661
text = "".join(text).strip() if text else ""
7762
frame_locals = (
@@ -272,10 +257,8 @@ def _stack_frames(depth=1):
272257

273258

274259
class _StackTraceRecorder:
275-
def __init__(self, excluded_paths):
276-
self.excluded_paths = excluded_paths
260+
def __init__(self):
277261
self.filename_cache = {}
278-
self.is_excluded_cache = {}
279262

280263
def get_source_file(self, frame):
281264
frame_filename = frame.f_code.co_filename
@@ -296,25 +279,14 @@ def get_source_file(self, frame):
296279

297280
return value
298281

299-
def is_excluded_path(self, path):
300-
excluded = self.is_excluded_cache.get(path)
301-
if excluded is None:
302-
resolved_path = os.path.realpath(path)
303-
excluded = any(
304-
resolved_path.startswith(excluded_path)
305-
for excluded_path in self.excluded_paths
306-
)
307-
self.is_excluded_cache[path] = excluded
308-
return excluded
309-
310-
def get_stack_trace(self, include_locals=False, depth=1):
282+
def get_stack_trace(self, *, excluded_modules=None, include_locals=False, depth=1):
311283
trace = []
312284
for frame in _stack_frames(depth=depth + 1):
313-
filename, is_source = self.get_source_file(frame)
314-
315-
if self.is_excluded_path(filename):
285+
if _is_excluded_frame(frame, excluded_modules):
316286
continue
317287

288+
filename, is_source = self.get_source_file(frame)
289+
318290
line_no = frame.f_lineno
319291
func_name = frame.f_code.co_name
320292

@@ -334,14 +306,15 @@ def get_stack_trace(self, include_locals=False, depth=1):
334306
return trace
335307

336308

337-
def get_stack_trace(depth=1):
309+
def get_stack_trace(*, depth=1):
338310
config = dt_settings.get_config()
339311
if config["ENABLE_STACKTRACES"]:
340312
stack_trace_recorder = getattr(_local_data, "stack_trace_recorder", None)
341313
if stack_trace_recorder is None:
342-
stack_trace_recorder = _StackTraceRecorder(hidden_paths)
314+
stack_trace_recorder = _StackTraceRecorder()
343315
_local_data.stack_trace_recorder = stack_trace_recorder
344316
return stack_trace_recorder.get_stack_trace(
317+
excluded_modules=config["HIDE_IN_STACKTRACES"],
345318
include_locals=config["ENABLE_STACKTRACES_LOCALS"],
346319
depth=depth,
347320
)

0 commit comments

Comments
 (0)