1
0
mirror of https://github.com/django/django.git synced 2025-06-08 21:19: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()
# 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 = "...<trimmed %d bytes string>" % diff
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.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

View File

@ -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 = "&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)
@ -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):