From 9c1458d9ee4ec1bcccc6e4ed6244c2ef408cdafd Mon Sep 17 00:00:00 2001 From: Keerthi Vasan Date: Fri, 2 Feb 2024 12:29:19 +0530 Subject: [PATCH] fix: add a threshold limit for variable size in pretty printing exception report --- django/views/debug.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/django/views/debug.py b/django/views/debug.py index c1265bfe6b..9d49f3a0c9 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -2,6 +2,7 @@ import functools import inspect import itertools import re +import reprlib import sys import types import warnings @@ -310,6 +311,9 @@ class SafeExceptionReporterFilter: class ExceptionReporter: """Organize and coordinate reporting on exceptions.""" + repr_instance = reprlib.Repr(maxstring=4096, maxlist=1000) + MAX_VAR_SIZE_PRETTY_PRINT = 512 * 1024 # 512KB + @property def html_template_path(self): return builtin_template_path("technical_500.html") @@ -348,11 +352,15 @@ class ExceptionReporter: self.postmortem = self.exc_value.chain or [self.exc_value] frames = self.get_traceback_frames() + for i, frame in enumerate(frames): if "vars" in frame: frame_vars = [] for k, v in frame["vars"]: - v = pprint(v) + if sys.getsizeof(v) > self.MAX_VAR_SIZE_PRETTY_PRINT: + v = self.repr_instance.repr(v) + else: + v = pprint(v) # Trim large blobs of data if len(v) > 4096: v = "%s… " % (v[0:4096], len(v))