diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index e23b33c433..9295fdcf9c 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -30,6 +30,9 @@ from .library import Library register = Library() +# The number of characters that will be printed +# before trimming while printing an exception in reports. +EXCEPTION_PRINT_LIMIT = 4096 ####################### # STRING DECORATOR # @@ -970,16 +973,14 @@ def phone2numeric_filter(value): return phone2numeric(value) -PRINT_LIMIT = 4096 -repr_instance = DjangoRepr() -repr_instance.config(PRINT_LIMIT) - - @register.filter(is_safe=True) def pprint(v): + repr_instance = DjangoRepr() + repr_instance.config(limit=EXCEPTION_PRINT_LIMIT) + try: - if isinstance(v, Sized) and len(v) > PRINT_LIMIT: - diff = len(v) - PRINT_LIMIT + if isinstance(v, Sized) and len(v) > EXCEPTION_PRINT_LIMIT: + diff = len(v) - EXCEPTION_PRINT_LIMIT repr_instance.fillvalue = "..." % diff v = repr_instance.repr(v) diff --git a/django/views/debug.py b/django/views/debug.py index 0e3653cc0c..a7de456900 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -17,7 +17,6 @@ from django.utils.datastructures import MultiValueDict from django.utils.encoding import force_str from django.utils.module_loading import import_string from django.utils.regex_helper import _lazy_re_compile -from django.utils.repr import DjangoRepr from django.utils.version import PY311, get_docs_version from django.views.decorators.debug import coroutine_functions_to_sensitive_variables @@ -311,9 +310,6 @@ class SafeExceptionReporterFilter: class ExceptionReporter: """Organize and coordinate reporting on exceptions.""" - repr_instance = DjangoRepr() - PRINT_LIMIT = 4096 - @property def html_template_path(self): return builtin_template_path("technical_500.html") @@ -334,8 +330,6 @@ class ExceptionReporter: self.template_does_not_exist = False self.postmortem = None - self.repr_instance.config(limit=ExceptionReporter.PRINT_LIMIT) - def _get_raw_insecure_uri(self): """ Return an absolute URI from variables available in this request. Skip diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index cb87778f62..733e273c30 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -17,6 +17,7 @@ from django.db import DatabaseError, connection from django.http import Http404, HttpRequest, HttpResponse from django.shortcuts import render from django.template import TemplateDoesNotExist +from django.template.defaultfilters import EXCEPTION_PRINT_LIMIT from django.test import RequestFactory, SimpleTestCase, override_settings from django.test.utils import LoggingCaptureMixin from django.urls import path, reverse @@ -1084,7 +1085,7 @@ class ExceptionReporterTests(SimpleTestCase): html = reporter.get_traceback_html() self.assertEqual(len(html) // 1024 // 128, 0) # still fit in 128Kb trim_msg = "<trimmed %d bytes string>" % ( - large - ExceptionReporter.PRINT_LIMIT + repr_of_str_adds, + large - EXCEPTION_PRINT_LIMIT + repr_of_str_adds, ) self.assertIn(trim_msg, html) @@ -1108,7 +1109,7 @@ class ExceptionReporterTests(SimpleTestCase): ls = eval(v[:i] + "]") # Check if length of trimmed list is our limit - self.assertEqual(len(ls), ExceptionReporter.PRINT_LIMIT) + self.assertEqual(len(ls), EXCEPTION_PRINT_LIMIT) break def test_non_sizable_object(self):