Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
31 changes: 31 additions & 0 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,37 @@ def _safe_repr(self, object, context, maxlevels, level):
del context[objid]
return format % ", ".join(components), readable, recursive

if (issubclass(typ, set) and r is set.__repr__) or \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this block is almost a complete duplicate of the one above (please fix that).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged this section with the above one. Please let me know if this is satisfactory. I originally separated them to follow the layout of the function and separate out new code from old code.

(issubclass(typ, frozenset) and r is frozenset.__repr__):
if not object:
return repr(object), True, False
if typ is set:
format = "{%s}"
else:
format = typ.__name__ + "({%s})"
objid = id(object)
if maxlevels and level >= maxlevels:
return "{...}", False, objid in context
if objid in context:
return _recursion(object), False, True
context[objid] = 1
readable = True
recursive = False
components = []
append = components.append
level += 1
items = sorted(object, key=_safe_key)
for item in items:
orepr, oreadable, orecur = self.format(
item, context, maxlevels, level)
append("%s" % orepr)
if not oreadable:
readable = False
if orecur:
recursive = True
del context[objid]
return format % ", ".join(components), readable, recursive

rep = repr(object)
return rep, (rep and not rep.startswith('<')), False

Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,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,
Expand All @@ -482,6 +485,9 @@ 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,
Expand All @@ -496,6 +502,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,
Expand All @@ -504,6 +516,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,
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Gawain Bolton
Carl Friedrich Bolz-Tereick
Forest Bond
Gregory Bond
Ben Bonenfant
Angelin Booz
Médéric Boquien
Matias Bordese
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the `pprint` module to always order the contents of `set` and `frozenset` objects.