diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index ae4d49168..6b80c5af0 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -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 "" + directory = "" + filename = abspath html += format_html( ( - '{}/' + '{}' + '{} in' + ' {}' + '({})\n' diff --git a/tests/test_utils.py b/tests/test_utils.py index fa1312482..9cfc33bc7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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): @@ -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('/server/', result) + self.assertIn('app.py in', result) + + trace = [ + ( + "", + 1, + "foo", + ["code line 1", "code line 2"], + {"foo": "bar"}, + ) + ] + result = render_stacktrace(trace) + self.assertIn('', result) + self.assertIn( + '<frozen importlib._bootstrap> in', + result, + )