1
0
mirror of https://github.com/django/django.git synced 2025-06-13 07:29:13 +00:00

refactor: create constant for limit, move repr creation into pprint()

This commit is contained in:
Keerthi Vasan 2024-02-10 12:06:38 +05:30
parent 4a10aa1b4f
commit 03c45aa696
3 changed files with 11 additions and 15 deletions

View File

@ -30,6 +30,9 @@ from .library import Library
register = 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 # # STRING DECORATOR #
@ -970,16 +973,14 @@ def phone2numeric_filter(value):
return phone2numeric(value) return phone2numeric(value)
PRINT_LIMIT = 4096
repr_instance = DjangoRepr()
repr_instance.config(PRINT_LIMIT)
@register.filter(is_safe=True) @register.filter(is_safe=True)
def pprint(v): def pprint(v):
repr_instance = DjangoRepr()
repr_instance.config(limit=EXCEPTION_PRINT_LIMIT)
try: try:
if isinstance(v, Sized) and len(v) > PRINT_LIMIT: if isinstance(v, Sized) and len(v) > EXCEPTION_PRINT_LIMIT:
diff = len(v) - PRINT_LIMIT diff = len(v) - EXCEPTION_PRINT_LIMIT
repr_instance.fillvalue = "...<trimmed %d bytes string>" % diff repr_instance.fillvalue = "...<trimmed %d bytes string>" % diff
v = repr_instance.repr(v) v = repr_instance.repr(v)

View File

@ -17,7 +17,6 @@ from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
from django.utils.regex_helper import _lazy_re_compile 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.utils.version import PY311, get_docs_version
from django.views.decorators.debug import coroutine_functions_to_sensitive_variables from django.views.decorators.debug import coroutine_functions_to_sensitive_variables
@ -311,9 +310,6 @@ class SafeExceptionReporterFilter:
class ExceptionReporter: class ExceptionReporter:
"""Organize and coordinate reporting on exceptions.""" """Organize and coordinate reporting on exceptions."""
repr_instance = DjangoRepr()
PRINT_LIMIT = 4096
@property @property
def html_template_path(self): def html_template_path(self):
return builtin_template_path("technical_500.html") return builtin_template_path("technical_500.html")
@ -334,8 +330,6 @@ class ExceptionReporter:
self.template_does_not_exist = False self.template_does_not_exist = False
self.postmortem = None self.postmortem = None
self.repr_instance.config(limit=ExceptionReporter.PRINT_LIMIT)
def _get_raw_insecure_uri(self): def _get_raw_insecure_uri(self):
""" """
Return an absolute URI from variables available in this request. Skip Return an absolute URI from variables available in this request. Skip

View File

@ -17,6 +17,7 @@ from django.db import DatabaseError, connection
from django.http import Http404, HttpRequest, HttpResponse from django.http import Http404, HttpRequest, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from django.template import TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.template.defaultfilters import EXCEPTION_PRINT_LIMIT
from django.test import RequestFactory, SimpleTestCase, override_settings from django.test import RequestFactory, SimpleTestCase, override_settings
from django.test.utils import LoggingCaptureMixin from django.test.utils import LoggingCaptureMixin
from django.urls import path, reverse from django.urls import path, reverse
@ -1084,7 +1085,7 @@ class ExceptionReporterTests(SimpleTestCase):
html = reporter.get_traceback_html() html = reporter.get_traceback_html()
self.assertEqual(len(html) // 1024 // 128, 0) # still fit in 128Kb self.assertEqual(len(html) // 1024 // 128, 0) # still fit in 128Kb
trim_msg = "&lt;trimmed %d bytes string&gt;" % ( trim_msg = "&lt;trimmed %d bytes string&gt;" % (
large - ExceptionReporter.PRINT_LIMIT + repr_of_str_adds, large - EXCEPTION_PRINT_LIMIT + repr_of_str_adds,
) )
self.assertIn(trim_msg, html) self.assertIn(trim_msg, html)
@ -1108,7 +1109,7 @@ class ExceptionReporterTests(SimpleTestCase):
ls = eval(v[:i] + "]") ls = eval(v[:i] + "]")
# Check if length of trimmed list is our limit # Check if length of trimmed list is our limit
self.assertEqual(len(ls), ExceptionReporter.PRINT_LIMIT) self.assertEqual(len(ls), EXCEPTION_PRINT_LIMIT)
break break
def test_non_sizable_object(self): def test_non_sizable_object(self):