summaryrefslogtreecommitdiffstats
path: root/Lib/reprlib.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/reprlib.py')
-rw-r--r--Lib/reprlib.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/Lib/reprlib.py b/Lib/reprlib.py
index c33b4da..a92b3e3 100644
--- a/Lib/reprlib.py
+++ b/Lib/reprlib.py
@@ -38,7 +38,7 @@ class Repr:
def __init__(
self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4,
maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40,
- maxother=30, fillvalue='...',
+ maxother=30, fillvalue='...', indent=None,
):
self.maxlevel = maxlevel
self.maxtuple = maxtuple
@@ -52,6 +52,7 @@ class Repr:
self.maxlong = maxlong
self.maxother = maxother
self.fillvalue = fillvalue
+ self.indent = indent
def repr(self, x):
return self.repr1(x, self.maxlevel)
@@ -66,6 +67,26 @@ class Repr:
else:
return self.repr_instance(x, level)
+ def _join(self, pieces, level):
+ if self.indent is None:
+ return ', '.join(pieces)
+ if not pieces:
+ return ''
+ indent = self.indent
+ if isinstance(indent, int):
+ if indent < 0:
+ raise ValueError(
+ f'Repr.indent cannot be negative int (was {indent!r})'
+ )
+ indent *= ' '
+ try:
+ sep = ',\n' + (self.maxlevel - level + 1) * indent
+ except TypeError as error:
+ raise TypeError(
+ f'Repr.indent must be a str, int or None, not {type(indent)}'
+ ) from error
+ return sep.join(('', *pieces, ''))[1:-len(indent) or None]
+
def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
n = len(x)
if level <= 0 and n:
@@ -76,8 +97,8 @@ class Repr:
pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
if n > maxiter:
pieces.append(self.fillvalue)
- s = ', '.join(pieces)
- if n == 1 and trail:
+ s = self._join(pieces, level)
+ if n == 1 and trail and self.indent is None:
right = trail + right
return '%s%s%s' % (left, s, right)
@@ -124,7 +145,7 @@ class Repr:
pieces.append('%s: %s' % (keyrepr, valrepr))
if n > self.maxdict:
pieces.append(self.fillvalue)
- s = ', '.join(pieces)
+ s = self._join(pieces, level)
return '{%s}' % (s,)
def repr_str(self, x, level):