1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

fix: apply suggested changes, move to text module

This commit is contained in:
Keerthi Vasan 2024-02-23 22:29:42 +05:30
parent c7f0d1f0bc
commit f6a198fe60
4 changed files with 47 additions and 37 deletions

View File

@ -17,9 +17,8 @@ from django.utils.html import avoid_wrapping, conditional_escape, escape, escape
from django.utils.html import json_script as _json_script
from django.utils.html import linebreaks, strip_tags
from django.utils.html import urlize as _urlize
from django.utils.repr import DjangoRepr
from django.utils.safestring import SafeData, mark_safe
from django.utils.text import Truncator, normalize_newlines, phone2numeric
from django.utils.text import DebugRepr, Truncator, normalize_newlines, phone2numeric
from django.utils.text import slugify as _slugify
from django.utils.text import wrap
from django.utils.timesince import timesince, timeuntil
@ -975,16 +974,16 @@ def phone2numeric_filter(value):
@register.filter(is_safe=True)
def pprint(value):
repr_instance = DjangoRepr()
repr_instance.config(limit=EXCEPTION_PRINT_LIMIT)
"""A wrapper with custom Repr implementation for debugging."""
repr_instance = DebugRepr(limit=EXCEPTION_PRINT_LIMIT)
try:
if isinstance(v, Sized) and len(v) > EXCEPTION_PRINT_LIMIT:
diff = len(v) - EXCEPTION_PRINT_LIMIT
if isinstance(value, Sized) and len(value) > EXCEPTION_PRINT_LIMIT:
diff = len(value) - EXCEPTION_PRINT_LIMIT
repr_instance.fillvalue = "...<trimmed %d bytes string>" % diff
v = repr_instance.repr(v)
value = repr_instance.repr(value)
except Exception as e:
v = "Error in formatting: %s: %s" % (e.__class__.__name__, e)
value = "Error in formatting: %s: %s" % (e.__class__.__name__, e)
return v
return value

View File

@ -1,26 +0,0 @@
import builtins
import reprlib
class DjangoRepr(reprlib.Repr):
def config(self, limit):
"""Set maximum print length for all data structures to `limit`."""
self.limit = limit
for attr in dir(self):
if attr.startswith("max") and attr != "maxlevel":
setattr(self, attr, limit)
def repr_str(self, x, level):
return "'%s'" % (x[: self.maxstring] + self.gen_trim_msg(len(x)))
def repr_instance(self, x, level):
s = builtins.repr(x)
if len(s) > self.maxother:
return s[: self.maxother] + self.gen_trim_msg(len(s))
return s
def gen_trim_msg(self, length):
if length <= self.limit:
return ""
return "...<trimmed %d bytes string>" % (length - self.limit)

View File

@ -1,5 +1,7 @@
import builtins
import gzip
import re
import reprlib
import secrets
import unicodedata
from gzip import GzipFile
@ -469,3 +471,38 @@ def _format_lazy(format_string, *args, **kwargs):
format_lazy = lazy(_format_lazy, str)
class DebugRepr(reprlib.Repr):
def __init__(self, limit):
"""Sets maximum print length for all data structures using the given value"""
self.maxlevel = limit
self.maxtuple = limit
self.maxlist = limit
self.maxarray = limit
self.maxdict = limit
self.maxset = limit
self.maxfrozenset = limit
self.maxdeque = limit
self.maxstring = limit
self.maxlong = limit
self.maxother = limit
self.limit = limit
self.fillvalue = ""
self.indent = 2
def repr_str(self, x, level):
return "'%s'" % (x[: self.maxstring] + self.gen_trim_msg(len(x)))
def repr_instance(self, x, level):
s = builtins.repr(x)
if len(s) > self.maxother:
return s[: self.maxother] + self.gen_trim_msg(len(s))
return s
def gen_trim_msg(self, length):
if length <= self.limit:
return ""
return "...<trimmed %d bytes string>" % (length - self.limit)

View File

@ -1037,7 +1037,6 @@ class ExceptionReporterTests(SimpleTestCase):
def test_local_variable_escaping(self):
"""Safe strings in local variables are escaped."""
try:
local = mark_safe("<p>Local variable</p>")
raise ValueError(local)
@ -1047,7 +1046,8 @@ class ExceptionReporterTests(SimpleTestCase):
html = ExceptionReporter(None, exc_type, exc_value, tb).get_traceback_html()
self.assertIn(
'<td class="code"><pre>&#x27;&lt;p&gt;Local variable&lt;/p&gt;&#x27;</pre>',
'<td class="code"><pre>&#x27;&lt;p&gt;Local variable&lt;/p&gt;&#x27;</pre>'
"</td>",
html,
)