node13h / django-debug-toolbar-template-profiler

Displays template rendering time on the timeline
Other
72 stars 14 forks source link

Handle possible division by zero in _calc_p #15

Closed micjabbour closed 4 years ago

micjabbour commented 4 years ago

Thanks for the great work 😄 The following minimal django app causes the plugin to raise a ZeroDivisionError:

# self-contained django app based on 
# https://github.com/wsvincent/django-microframework#option-2

from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.core.management import execute_from_command_line
from django.http import HttpResponse
from django.urls import include, path
import debug_toolbar
from django.template import Template, Context

settings.configure(
    ROOT_URLCONF=__name__,
    DEBUG=True,
    INSTALLED_APPS=[
        'django.contrib.staticfiles',
        'debug_toolbar',
        'template_profiler_panel',
    ],
    TEMPLATES=[
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'APP_DIRS': True,
        }
    ],
    STATIC_URL='/static/',
    MIDDLEWARE=['debug_toolbar.middleware.DebugToolbarMiddleware'],
    INTERNAL_IPS=['127.0.0.1'],
    DEBUG_TOOLBAR_PANELS=[
        'debug_toolbar.panels.timer.TimerPanel',
        'template_profiler_panel.panels.template.TemplateProfilerPanel',
    ],
)

def empty_page(request):
    t = Template("<html><body></body></html>")
    return HttpResponse(t.render(Context({})))

urlpatterns = [
    path('__debug__/', include(debug_toolbar.urls)),
    path('', empty_page)
]

application = WSGIHandler()

if __name__ == "__main__":
    execute_from_command_line()

Steps:

  1. Save the above code snippet into a file main.py.
  2. Run python main.py runserver.
  3. Open the page in the browser http://127.0.0.1:8000/.
  4. Keep refreshing the page until it eventually loads in 0.0 seconds and the plugin crashes.

This PR fixes the crash by changing _calc_p to return 100% when whole is 0.

adamchainz commented 4 years ago

Released in 2.0.1.