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

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.

A new assert has been added to `test_reprlib.py` to verify the new
attributes’ functionality.
This commit is contained in:
Alexander Böhn 2020-02-03 21:11:28 -05:00
parent 5807efd4c3
commit c27e567b5e
No known key found for this signature in database
GPG Key ID: 2482B5C9F12EB5D8
3 changed files with 26 additions and 9 deletions

View File

@ -36,6 +36,7 @@ def recursive_repr(fillvalue='...'):
class Repr:
def __init__(self):
self.fillvalue = '...'
self.maxlevel = 6
self.maxtuple = 6
self.maxlist = 6
@ -64,14 +65,16 @@ class Repr:
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):
@ -104,8 +107,10 @@ class Repr:
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 = []
@ -113,7 +118,8 @@ class Repr:
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,)
@ -123,7 +129,7 @@ class Repr:
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):
@ -131,7 +137,7 @@ class Repr:
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):
@ -144,7 +150,7 @@ class Repr:
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

View File

@ -50,6 +50,13 @@ class ReprTests(unittest.TestCase):
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

View File

@ -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.