1
0
mirror of https://github.com/django/django.git synced 2025-06-07 20:49:11 +00:00

refactor: move repr fill value generation to custom class

This commit is contained in:
Keerthi Vasan 2024-02-23 23:10:32 +05:30
parent 4182d06f7c
commit cf43a55647
2 changed files with 7 additions and 4 deletions

View File

@ -3,7 +3,6 @@
import random as random_module import random as random_module
import re import re
import types import types
from collections.abc import Sized
from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation, getcontext from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation, getcontext
from functools import wraps from functools import wraps
from inspect import unwrap from inspect import unwrap
@ -978,9 +977,6 @@ def pprint(value):
repr_instance = DebugRepr(limit=EXCEPTION_PRINT_LIMIT) repr_instance = DebugRepr(limit=EXCEPTION_PRINT_LIMIT)
try: try:
if isinstance(value, Sized) and len(value) > EXCEPTION_PRINT_LIMIT:
diff = len(value) - EXCEPTION_PRINT_LIMIT
repr_instance.fillvalue = "...<trimmed %d bytes string>" % diff
value = repr_instance.repr(value) value = repr_instance.repr(value)
except Exception as e: except Exception as e:

View File

@ -4,6 +4,7 @@ import re
import reprlib import reprlib
import secrets import secrets
import unicodedata import unicodedata
from collections.abc import Sized
from gzip import GzipFile from gzip import GzipFile
from gzip import compress as gzip_compress from gzip import compress as gzip_compress
from io import BytesIO from io import BytesIO
@ -502,6 +503,12 @@ class DebugRepr(reprlib.Repr):
return s[: self.maxother] + self.gen_trim_msg(len(s)) return s[: self.maxother] + self.gen_trim_msg(len(s))
return s return s
def print(self, value):
if isinstance(value, Sized) and len(value) > self.limit:
length = len(value)
self.fillvalue = self.gen_trim_msg(length)
return self.repr(value)
def gen_trim_msg(self, length): def gen_trim_msg(self, length):
if length <= self.limit: if length <= self.limit:
return "" return ""