Skip to content

bpo-39549: reprlib.Repr uses a “fillvalue” attribute #18343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/library/reprlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ size limits for the representations of different object types, and methods
which format specific object types.


.. attribute:: Repr.fillvalue

This string is displayed for recursive references. It defaults to
``...``.

.. versionadded:: 3.11


.. attribute:: Repr.maxlevel

Depth limit on the creation of recursive representations. The default is ``6``.
Expand Down
24 changes: 15 additions & 9 deletions Lib/reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def wrapper(self):
class Repr:

def __init__(self):
self.fillvalue = '...'
self.maxlevel = 6
self.maxtuple = 6
self.maxlist = 6
Expand Down Expand Up @@ -64,14 +65,16 @@ def repr1(self, x, level):
def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
n = len(x)
if level <= 0 and n:
s = '...'
s = self.fillvalue
else:
newlevel = level - 1
repr1 = self.repr1
pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
if n > maxiter: pieces.append('...')
if n > maxiter:
pieces.append(self.fillvalue)
s = ', '.join(pieces)
if n == 1 and trail: right = trail + right
if n == 1 and trail:
right = trail + right
return '%s%s%s' % (left, s, right)

def repr_tuple(self, x, level):
Expand Down Expand Up @@ -104,16 +107,19 @@ def repr_deque(self, x, level):

def repr_dict(self, x, level):
n = len(x)
if n == 0: return '{}'
if level <= 0: return '{...}'
if n == 0:
return '{}'
if level <= 0:
return '{' + self.fillvalue + '}'
newlevel = level - 1
repr1 = self.repr1
pieces = []
for key in islice(_possibly_sorted(x), self.maxdict):
keyrepr = repr1(key, newlevel)
valrepr = repr1(x[key], newlevel)
pieces.append('%s: %s' % (keyrepr, valrepr))
if n > self.maxdict: pieces.append('...')
if n > self.maxdict:
pieces.append(self.fillvalue)
s = ', '.join(pieces)
return '{%s}' % (s,)

Expand All @@ -123,15 +129,15 @@ def repr_str(self, x, level):
i = max(0, (self.maxstring-3)//2)
j = max(0, self.maxstring-3-i)
s = builtins.repr(x[:i] + x[len(x)-j:])
s = s[:i] + '...' + s[len(s)-j:]
s = s[:i] + self.fillvalue + s[len(s)-j:]
return s

def repr_int(self, x, level):
s = builtins.repr(x) # XXX Hope this isn't too slow...
if len(s) > self.maxlong:
i = max(0, (self.maxlong-3)//2)
j = max(0, self.maxlong-3-i)
s = s[:i] + '...' + s[len(s)-j:]
s = s[:i] + self.fillvalue + s[len(s)-j:]
return s

def repr_instance(self, x, level):
Expand All @@ -144,7 +150,7 @@ def repr_instance(self, x, level):
if len(s) > self.maxother:
i = max(0, (self.maxother-3)//2)
j = max(0, self.maxother-3-i)
s = s[:i] + '...' + s[len(s)-j:]
s = s[:i] + self.fillvalue + s[len(s)-j:]
return s


Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def test_tuple(self):
expected = repr(t3)[:-2] + "...)"
eq(r2.repr(t3), expected)

# modified fillvalue:
r3 = Repr()
r3.fillvalue = '+++'
r3.maxtuple = 2
expected = repr(t3)[:-2] + "+++)"
eq(r3.repr(t3), expected)

def test_container(self):
from array import array
from collections import deque
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Whereas the code for reprlib.Repr had previously used a hardcoded string
value of '...', this PR updates it to use of a “fillvalue” attribute, whose
value defaults to '...' and can be reset in either individual reprlib.Repr
instances or in subclasses thereof.