diff --git a/Lib/pprint.py b/Lib/pprint.py index dc0953cec67a58..826935ef15c6e4 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -611,17 +611,29 @@ def _safe_repr(self, object, context, maxlevels, level): return "{%s}" % ", ".join(components), readable, recursive if (issubclass(typ, list) and r is list.__repr__) or \ - (issubclass(typ, tuple) and r is tuple.__repr__): + (issubclass(typ, tuple) and r is tuple.__repr__) or \ + (issubclass(typ, set) and r is set.__repr__) or \ + (issubclass(typ, frozenset) and r is frozenset.__repr__): if issubclass(typ, list): if not object: return "[]", True, False format = "[%s]" - elif len(object) == 1: - format = "(%s,)" + elif issubclass(typ, tuple): + if len(object) == 1: + format = "(%s,)" + else: + if not object: + return "()", True, False + format = "(%s)" else: if not object: - return "()", True, False - format = "(%s)" + return repr(object), True, False + if typ is set: + format = "{%s}" + else: + format = typ.__name__ + "({%s})" + object = sorted(object, key=_safe_key) + objid = id(object) if maxlevels and level >= maxlevels: return format % "...", False, objid in context diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index dfbc2a06e7346f..9d65a4872b3dfc 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -580,6 +580,9 @@ def test_subclassing(self): def test_set_reprs(self): self.assertEqual(pprint.pformat(set()), 'set()') self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}') + self.assertEqual(pprint.pformat(set([-1, 0, 1])), "{-1, 0, 1}") + self.assertEqual(pprint.pformat(set("0123")), "{'0', '1', '2', '3'}") + self.assertEqual(pprint.pformat(set("abcd")), "{'a', 'b', 'c', 'd'}") self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\ {0, 1, @@ -588,6 +591,11 @@ def test_set_reprs(self): 4, 5, 6}''') + self.assertEqual(pprint.pformat(set2([-1, 0, 1])), "set2({-1, 0, 1})") + self.assertEqual(pprint.pformat(set2("0123")), + "set2({'0', '1', '2', '3'})") + self.assertEqual(pprint.pformat(set2("abcd")), + "set2({'a', 'b', 'c', 'd'})") self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\ set2({0, 1, @@ -602,6 +610,12 @@ def test_set_reprs(self): self.assertEqual(pprint.pformat(frozenset()), 'frozenset()') self.assertEqual(pprint.pformat(frozenset(range(3))), 'frozenset({0, 1, 2})') + self.assertEqual(pprint.pformat(frozenset([-1, 0, 1])), + "frozenset({-1, 0, 1})") + self.assertEqual(pprint.pformat(frozenset("0123")), + "frozenset({'0', '1', '2', '3'})") + self.assertEqual(pprint.pformat(frozenset("abcd")), + "frozenset({'a', 'b', 'c', 'd'})") self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\ frozenset({0, 1, @@ -610,6 +624,12 @@ def test_set_reprs(self): 4, 5, 6})''') + self.assertEqual(pprint.pformat(frozenset2([-1, 0, 1])), + "frozenset2({-1, 0, 1})") + self.assertEqual(pprint.pformat(frozenset2("0123")), + "frozenset2({'0', '1', '2', '3'})") + self.assertEqual(pprint.pformat(frozenset2("abcd")), + "frozenset2({'a', 'b', 'c', 'd'})") self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\ frozenset2({0, 1, diff --git a/Misc/ACKS b/Misc/ACKS index 913f7c8ecf5f1e..ed963053b7001e 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -201,6 +201,7 @@ Gawain Bolton Carl Friedrich Bolz-Tereick Forest Bond Gregory Bond +Ben Bonenfant David Bonner Angelin Booz Médéric Boquien diff --git a/Misc/NEWS.d/next/Library/2020-10-26-14-20-58.bpo-27495.OWavSt.rst b/Misc/NEWS.d/next/Library/2020-10-26-14-20-58.bpo-27495.OWavSt.rst new file mode 100644 index 00000000000000..d444cbdf9e2ebb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-26-14-20-58.bpo-27495.OWavSt.rst @@ -0,0 +1,2 @@ +Update the :mod:`pprint` module to always order the contents of :class:`set` +and :class:`frozenset` objects.