Skip to content

The path may not always be a true path for stacktraces. #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,17 @@ def render_stacktrace(trace):
show_locals = dt_settings.get_config()["ENABLE_STACKTRACES_LOCALS"]
html = ""
for abspath, lineno, func, code, locals_ in trace:
directory, filename = abspath.rsplit(os.path.sep, 1)
if os.path.sep in abspath:
directory, filename = abspath.rsplit(os.path.sep, 1)
# We want the separator to appear in the UI so add it back.
directory += os.path.sep
else:
# abspath could be something like "<frozen importlib._bootstrap>"
directory = ""
filename = abspath
html += format_html(
(
'<span class="djdt-path">{}/</span>'
'<span class="djdt-path">{}</span>'
+ '<span class="djdt-file">{}</span> in'
+ ' <span class="djdt-func">{}</span>'
+ '(<span class="djdt-lineno">{}</span>)\n'
Expand Down
28 changes: 27 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from debug_toolbar.utils import get_name_from_obj
from debug_toolbar.utils import get_name_from_obj, render_stacktrace


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

res = get_name_from_obj(A)
self.assertEqual(res, "tests.test_utils.A")


class RenderStacktraceTestCase(unittest.TestCase):
def test_importlib_path_issue_1612(self):
trace = [
("/server/app.py", 1, "foo", ["code line 1", "code line 2"], {"foo": "bar"})
]
result = render_stacktrace(trace)
self.assertIn('<span class="djdt-path">/server/</span>', result)
self.assertIn('<span class="djdt-file">app.py</span> in', result)

trace = [
(
"<frozen importlib._bootstrap>",
1,
"foo",
["code line 1", "code line 2"],
{"foo": "bar"},
)
]
result = render_stacktrace(trace)
self.assertIn('<span class="djdt-path"></span>', result)
self.assertIn(
'<span class="djdt-file">&lt;frozen importlib._bootstrap&gt;</span> in',
result,
)