[Python-checkins] cpython: Issue #22824: Updated reprlib output format for sets to use set literals.

raymond.hettinger python-checkins at python.org
Mon Nov 10 07:30:42 CET 2014


https://hg.python.org/cpython/rev/147fda13bec8
changeset:   93449:147fda13bec8
parent:      93447:55b89e5f9c37
user:        Raymond Hettinger <python at rcn.com>
date:        Sun Nov 09 22:30:36 2014 -0800
summary:
  Issue #22824:  Updated reprlib output format for sets to use set literals.

files:
  Doc/tutorial/stdlib2.rst |   2 +-
  Lib/reprlib.py           |   8 +++-
  Lib/test/test_reprlib.py |  44 ++++++++++++++++-----------
  Misc/NEWS                |   3 +
  4 files changed, 36 insertions(+), 21 deletions(-)


diff --git a/Doc/tutorial/stdlib2.rst b/Doc/tutorial/stdlib2.rst
--- a/Doc/tutorial/stdlib2.rst
+++ b/Doc/tutorial/stdlib2.rst
@@ -18,7 +18,7 @@
 
    >>> 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.
diff --git a/Lib/reprlib.py b/Lib/reprlib.py
--- a/Lib/reprlib.py
+++ b/Lib/reprlib.py
@@ -87,12 +87,16 @@
         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):
diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py
--- a/Lib/test/test_reprlib.py
+++ b/Lib/test/test_reprlib.py
@@ -10,7 +10,7 @@
 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 @@
         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 @@
         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 @@
         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()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -186,6 +186,9 @@
 - 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list