From 0900815097f75a89132b7a2f25b0cac135a49b82 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 18 Nov 2014 21:52:58 +0100 Subject: [PATCH] Simplified caching of the default exception reporter filter. Also simplified the logic under the assumption that a false-ish object won't have an exception_reporter_filter attribute. --- django/views/debug.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/django/views/debug.py b/django/views/debug.py index 59136ce90c..06a27069f6 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -16,6 +16,7 @@ from django.template.loaders.utils import get_template_loaders from django.utils.datastructures import MultiValueDict from django.utils.html import escape from django.utils.encoding import force_bytes, smart_text +from django.utils import lru_cache from django.utils.module_loading import import_string from django.utils import six from django.utils.translation import ugettext as _ @@ -94,20 +95,16 @@ def technical_500_response(request, exc_type, exc_value, tb, status_code=500): html = reporter.get_traceback_html() return HttpResponse(html, status=status_code, content_type='text/html') -# Cache for the default exception reporter filter instance. -default_exception_reporter_filter = None + +@lru_cache.lru_cache() +def get_default_exception_reporter_filter(): + # Instantiate the default filter for the first time and cache it. + return import_string(settings.DEFAULT_EXCEPTION_REPORTER_FILTER)() def get_exception_reporter_filter(request): - global default_exception_reporter_filter - if default_exception_reporter_filter is None: - # Load the default filter for the first time and cache it. - default_exception_reporter_filter = import_string( - settings.DEFAULT_EXCEPTION_REPORTER_FILTER)() - if request: - return getattr(request, 'exception_reporter_filter', default_exception_reporter_filter) - else: - return default_exception_reporter_filter + default_filter = get_default_exception_reporter_filter() + return getattr(request, 'exception_reporter_filter', default_filter) class ExceptionReporterFilter(object):