Skip to content

Commit c440069

Browse files
authored
Merge pull request django-commons#988 from radwon/ticket_792
Fixed django-commons#792 -- KeyError in ProfilingPanel
2 parents 88ddc7b + 3b0da34 commit c440069

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

debug_toolbar/panels/profiling.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ def generate_stats(self, request, response):
167167
self.stats = DjangoDebugToolbarStats(self.profiler)
168168
self.stats.calc_callees()
169169

170-
root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)
171-
172-
func_list = []
173-
self.add_node(func_list,
174-
root,
175-
dt_settings.get_config()['PROFILER_MAX_DEPTH'],
176-
root.stats[3] / 8)
177-
178-
self.record_stats({'func_list': func_list})
170+
root_func = self.stats.get_root_func()
171+
# Ensure root function exists before continuing with function call analysis
172+
if root_func:
173+
root = FunctionCall(self.stats, root_func, depth=0)
174+
func_list = []
175+
self.add_node(func_list,
176+
root,
177+
dt_settings.get_config()['PROFILER_MAX_DEPTH'],
178+
root.stats[3] / 8)
179+
self.record_stats({'func_list': func_list})

tests/panels/test_profiling.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ def test_listcomp_escaped(self):
4747
self.assertNotIn('<span class="djdt-func"><listcomp></span>', self.panel.content)
4848
self.assertIn('<span class="djdt-func">&lt;listcomp&gt;</span>', self.panel.content)
4949

50+
def test_generate_stats_no_profiler(self):
51+
"""
52+
Test generating stats with no profiler.
53+
"""
54+
self.assertIsNone(self.panel.generate_stats(self.request, self.response))
55+
56+
def test_generate_stats_no_root_func(self):
57+
"""
58+
Test generating stats using profiler without root function.
59+
"""
60+
self.panel.process_view(self.request, regular_view, ('profiling',), {})
61+
self.panel.process_response(self.request, self.response)
62+
self.panel.profiler.clear()
63+
self.panel.profiler.enable()
64+
self.panel.profiler.disable()
65+
self.panel.generate_stats(self.request, self.response)
66+
self.assertNotIn('func_list', self.panel.get_stats())
67+
5068

5169
@override_settings(DEBUG=True,
5270
DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingPanel'])

0 commit comments

Comments
 (0)