3
3
import os .path
4
4
import sys
5
5
import warnings
6
- from importlib import import_module
7
6
from pprint import pformat
8
7
9
- import django
10
8
from asgiref .local import Local
11
- from django .core .exceptions import ImproperlyConfigured
12
9
from django .template import Node
13
10
from django .utils .html import format_html
14
11
from django .utils .safestring import mark_safe
24
21
_local_data = Local ()
25
22
26
23
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
+ )
51
35
52
36
53
37
def _stack_trace_deprecation_warning ():
@@ -70,8 +54,9 @@ def tidy_stacktrace(stack):
70
54
_stack_trace_deprecation_warning ()
71
55
72
56
trace = []
57
+ excluded_modules = dt_settings .get_config ()["HIDE_IN_STACKTRACES" ]
73
58
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 ):
75
60
continue
76
61
text = "" .join (text ).strip () if text else ""
77
62
frame_locals = (
@@ -272,10 +257,8 @@ def _stack_frames(depth=1):
272
257
273
258
274
259
class _StackTraceRecorder :
275
- def __init__ (self , excluded_paths ):
276
- self .excluded_paths = excluded_paths
260
+ def __init__ (self ):
277
261
self .filename_cache = {}
278
- self .is_excluded_cache = {}
279
262
280
263
def get_source_file (self , frame ):
281
264
frame_filename = frame .f_code .co_filename
@@ -296,25 +279,14 @@ def get_source_file(self, frame):
296
279
297
280
return value
298
281
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 ):
311
283
trace = []
312
284
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 ):
316
286
continue
317
287
288
+ filename , is_source = self .get_source_file (frame )
289
+
318
290
line_no = frame .f_lineno
319
291
func_name = frame .f_code .co_name
320
292
@@ -334,14 +306,15 @@ def get_stack_trace(self, include_locals=False, depth=1):
334
306
return trace
335
307
336
308
337
- def get_stack_trace (depth = 1 ):
309
+ def get_stack_trace (* , depth = 1 ):
338
310
config = dt_settings .get_config ()
339
311
if config ["ENABLE_STACKTRACES" ]:
340
312
stack_trace_recorder = getattr (_local_data , "stack_trace_recorder" , None )
341
313
if stack_trace_recorder is None :
342
- stack_trace_recorder = _StackTraceRecorder (hidden_paths )
314
+ stack_trace_recorder = _StackTraceRecorder ()
343
315
_local_data .stack_trace_recorder = stack_trace_recorder
344
316
return stack_trace_recorder .get_stack_trace (
317
+ excluded_modules = config ["HIDE_IN_STACKTRACES" ],
345
318
include_locals = config ["ENABLE_STACKTRACES_LOCALS" ],
346
319
depth = depth ,
347
320
)
0 commit comments