Issue #22824: Updated reprlib output format for sets to use set literals.

This commit is contained in:
Raymond Hettinger 2014-11-09 22:30:36 -08:00
parent 223bc2fe16
commit ffd842e1d6
4 changed files with 36 additions and 21 deletions

View File

@ -18,7 +18,7 @@ abbreviated displays of large or deeply nested containers::
>>> import reprlib
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))
"set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
"{'a', 'c', 'd', 'e', 'f', 'g', ...}"
The :mod:`pprint` module offers more sophisticated control over printing both
built-in and user defined objects in a way that is readable by the interpreter.

View File

@ -87,12 +87,16 @@ class Repr:
return self._repr_iterable(x, level, header, '])', self.maxarray)
def repr_set(self, x, level):
if not x:
return 'set()'
x = _possibly_sorted(x)
return self._repr_iterable(x, level, 'set([', '])', self.maxset)
return self._repr_iterable(x, level, '{', '}', self.maxset)
def repr_frozenset(self, x, level):
if not x:
return 'frozenset()'
x = _possibly_sorted(x)
return self._repr_iterable(x, level, 'frozenset([', '])',
return self._repr_iterable(x, level, 'frozenset({', '})',
self.maxfrozenset)
def repr_deque(self, x, level):

View File

@ -10,7 +10,7 @@ import importlib
import importlib.util
import unittest
from test.support import run_unittest, create_empty_file, verbose
from test.support import create_empty_file, verbose
from reprlib import repr as r # Don't shadow builtin repr
from reprlib import Repr
from reprlib import recursive_repr
@ -70,18 +70,18 @@ class ReprTests(unittest.TestCase):
eq(r([1, 2, 3, 4, 5, 6, 7]), "[1, 2, 3, 4, 5, 6, ...]")
# Sets give up after 6 as well
eq(r(set([])), "set([])")
eq(r(set([1])), "set([1])")
eq(r(set([1, 2, 3])), "set([1, 2, 3])")
eq(r(set([1, 2, 3, 4, 5, 6])), "set([1, 2, 3, 4, 5, 6])")
eq(r(set([1, 2, 3, 4, 5, 6, 7])), "set([1, 2, 3, 4, 5, 6, ...])")
eq(r(set([])), "set()")
eq(r(set([1])), "{1}")
eq(r(set([1, 2, 3])), "{1, 2, 3}")
eq(r(set([1, 2, 3, 4, 5, 6])), "{1, 2, 3, 4, 5, 6}")
eq(r(set([1, 2, 3, 4, 5, 6, 7])), "{1, 2, 3, 4, 5, 6, ...}")
# Frozensets give up after 6 as well
eq(r(frozenset([])), "frozenset([])")
eq(r(frozenset([1])), "frozenset([1])")
eq(r(frozenset([1, 2, 3])), "frozenset([1, 2, 3])")
eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset([1, 2, 3, 4, 5, 6])")
eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset([1, 2, 3, 4, 5, 6, ...])")
eq(r(frozenset([])), "frozenset()")
eq(r(frozenset([1])), "frozenset({1})")
eq(r(frozenset([1, 2, 3])), "frozenset({1, 2, 3})")
eq(r(frozenset([1, 2, 3, 4, 5, 6])), "frozenset({1, 2, 3, 4, 5, 6})")
eq(r(frozenset([1, 2, 3, 4, 5, 6, 7])), "frozenset({1, 2, 3, 4, 5, 6, ...})")
# collections.deque after 6
eq(r(deque([1, 2, 3, 4, 5, 6, 7])), "deque([1, 2, 3, 4, 5, 6, ...])")
@ -103,6 +103,20 @@ class ReprTests(unittest.TestCase):
eq(r(array('i', [1, 2, 3, 4, 5, 6])),
"array('i', [1, 2, 3, 4, 5, ...])")
def test_set_literal(self):
eq = self.assertEqual
eq(r({1}), "{1}")
eq(r({1, 2, 3}), "{1, 2, 3}")
eq(r({1, 2, 3, 4, 5, 6}), "{1, 2, 3, 4, 5, 6}")
eq(r({1, 2, 3, 4, 5, 6, 7}), "{1, 2, 3, 4, 5, 6, ...}")
def test_frozenset(self):
eq = self.assertEqual
eq(r(frozenset({1})), "frozenset({1})")
eq(r(frozenset({1, 2, 3})), "frozenset({1, 2, 3})")
eq(r(frozenset({1, 2, 3, 4, 5, 6})), "frozenset({1, 2, 3, 4, 5, 6})")
eq(r(frozenset({1, 2, 3, 4, 5, 6, 7})), "frozenset({1, 2, 3, 4, 5, 6, ...})")
def test_numbers(self):
eq = self.assertEqual
eq(r(123), repr(123))
@ -373,11 +387,5 @@ class TestRecursiveRepr(unittest.TestCase):
m.append(m)
self.assertEqual(repr(m), '<a, b, c, d, e, +++, x, +++>')
def test_main():
run_unittest(ReprTests)
run_unittest(LongReprTest)
run_unittest(TestRecursiveRepr)
if __name__ == "__main__":
test_main()
unittest.main()

View File

@ -186,6 +186,9 @@ Library
- Issues #814253, #9179: Group references and conditional group references now
work in lookbehind assertions in regular expressions.
- Issue #22824: Updated reprlib output format for sets to use set literals.
Patch contributed by Berker Peksag.
- Issue #22406: Fixed the uu_codec codec incorrectly ported to 3.x.
Based on patch by Martin Panter.