Skip to content

Commit c39b84b

Browse files
The path may not always be a true path for stacktraces. (#1613)
Occassionally we will get a stacktrace that's an importlib instance string representation. While we may be able to put the python path or something else it's likely easier (and more logical) to simply pass that onto the user. I was unable to reproduce the issue in our tests, so I've mocked the case in test_importlib_path_issue_1612. Fixes #1612
1 parent 4630f07 commit c39b84b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

debug_toolbar/utils.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,17 @@ def render_stacktrace(trace):
7272
show_locals = dt_settings.get_config()["ENABLE_STACKTRACES_LOCALS"]
7373
html = ""
7474
for abspath, lineno, func, code, locals_ in trace:
75-
directory, filename = abspath.rsplit(os.path.sep, 1)
75+
if os.path.sep in abspath:
76+
directory, filename = abspath.rsplit(os.path.sep, 1)
77+
# We want the separator to appear in the UI so add it back.
78+
directory += os.path.sep
79+
else:
80+
# abspath could be something like "<frozen importlib._bootstrap>"
81+
directory = ""
82+
filename = abspath
7683
html += format_html(
7784
(
78-
'<span class="djdt-path">{}/</span>'
85+
'<span class="djdt-path">{}</span>'
7986
+ '<span class="djdt-file">{}</span> in'
8087
+ ' <span class="djdt-func">{}</span>'
8188
+ '(<span class="djdt-lineno">{}</span>)\n'

tests/test_utils.py

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

3-
from debug_toolbar.utils import get_name_from_obj
3+
from debug_toolbar.utils import get_name_from_obj, render_stacktrace
44

55

66
class GetNameFromObjTestCase(unittest.TestCase):
@@ -21,3 +21,29 @@ class A:
2121

2222
res = get_name_from_obj(A)
2323
self.assertEqual(res, "tests.test_utils.A")
24+
25+
26+
class RenderStacktraceTestCase(unittest.TestCase):
27+
def test_importlib_path_issue_1612(self):
28+
trace = [
29+
("/server/app.py", 1, "foo", ["code line 1", "code line 2"], {"foo": "bar"})
30+
]
31+
result = render_stacktrace(trace)
32+
self.assertIn('<span class="djdt-path">/server/</span>', result)
33+
self.assertIn('<span class="djdt-file">app.py</span> in', result)
34+
35+
trace = [
36+
(
37+
"<frozen importlib._bootstrap>",
38+
1,
39+
"foo",
40+
["code line 1", "code line 2"],
41+
{"foo": "bar"},
42+
)
43+
]
44+
result = render_stacktrace(trace)
45+
self.assertIn('<span class="djdt-path"></span>', result)
46+
self.assertIn(
47+
'<span class="djdt-file">&lt;frozen importlib._bootstrap&gt;</span> in',
48+
result,
49+
)

0 commit comments

Comments
 (0)