From 62307773e587a4151dd3c220adf6c94593374b02 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Mon, 2 May 2022 09:27:01 -0500 Subject: [PATCH 1/2] The path may not always be a true path for stacktraces. 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 --- debug_toolbar/utils.py | 10 ++++++++-- tests/test_utils.py | 28 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index ae4d49168..1ea80f22d 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -72,10 +72,16 @@ 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: + 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, + ) From 5b67bbbfd44492d4b9d6dad960842b785022325c Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Mon, 2 May 2022 11:16:12 -0500 Subject: [PATCH 2/2] Update debug_toolbar/utils.py Co-authored-by: Matthias Kestenholz --- debug_toolbar/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/debug_toolbar/utils.py b/debug_toolbar/utils.py index 1ea80f22d..6b80c5af0 100644 --- a/debug_toolbar/utils.py +++ b/debug_toolbar/utils.py @@ -77,6 +77,7 @@ def render_stacktrace(trace): # 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(