Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 0 additions & 6 deletions Lib/pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,6 @@ def _format(self, object, stream, indent, allowance, context, level):
p(self, object, stream, indent, allowance, context, level + 1)
del context[objid]
return
elif isinstance(object, dict):
context[objid] = 1
self._pprint_dict(object, stream, indent, allowance,
context, level + 1)
del context[objid]
return
stream.write(rep)

_dispatch = {}
Expand Down
49 changes: 48 additions & 1 deletion Lib/test/test_pprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,54 @@ class list3(list):
def __repr__(self):
return list.__repr__(self)

class list_custom_repr(list):
def __repr__(self):
return '*'*len(list.__repr__(self))

class tuple2(tuple):
pass

class tuple3(tuple):
def __repr__(self):
return tuple.__repr__(self)

class tuple_custom_repr(tuple):
def __repr__(self):
return '*'*len(tuple.__repr__(self))

class set2(set):
pass

class set3(set):
def __repr__(self):
return set.__repr__(self)

class set_custom_repr(set):
def __repr__(self):
return '*'*len(set.__repr__(self))

class frozenset2(frozenset):
pass

class frozenset3(frozenset):
def __repr__(self):
return frozenset.__repr__(self)

class frozenset_custom_repr(frozenset):
def __repr__(self):
return '*'*len(frozenset.__repr__(self))

class dict2(dict):
pass

class dict3(dict):
def __repr__(self):
return dict.__repr__(self)

class dict_custom_repr(dict):
def __repr__(self):
return '*'*len(dict.__repr__(self))

class Unorderable:
def __repr__(self):
return str(id(self))
Expand Down Expand Up @@ -155,7 +175,8 @@ def test_unreadable(self):
"expected not isreadable for %r" % (unreadable,))

def test_same_as_repr(self):
# Simple objects, small containers and classes that overwrite __repr__
# Simple objects, small containers and classes that override __repr__
# to directly call super's __repr__.
# For those the result should be the same as repr().
# Ahem. The docs don't say anything about that -- this appears to
# be testing an implementation quirk. Starting in Python 2.5, it's
Expand Down Expand Up @@ -187,6 +208,32 @@ def test_same_as_repr(self):
.replace('\n', ' '), native)
self.assertEqual(pprint.saferepr(simple), native)

def test_container_repr_override_called(self):
N = 1000
# Ensure that __repr__ override is called for subclasses of containers

for cont in (list_custom_repr(),
list_custom_repr([1,2,3]),
list_custom_repr(range(N)),
tuple_custom_repr(),
tuple_custom_repr([1,2,3]),
tuple_custom_repr(range(N)),
set_custom_repr(),
set_custom_repr([1,2,3]),
set_custom_repr(range(N)),
frozenset_custom_repr(),
frozenset_custom_repr([1,2,3]),
frozenset_custom_repr(range(N)),
dict_custom_repr(),
dict_custom_repr({5: 6}),
dict_custom_repr(zip(range(N),range(N))),
):
native = repr(cont)
expected = '*' * len(native)
self.assertEqual(pprint.pformat(cont), expected)
self.assertEqual(pprint.pformat(cont, width=1, indent=0), expected)
self.assertEqual(pprint.saferepr(cont), expected)

def test_basic_line_wrap(self):
# verify basic line-wrapping operation
o = {'RPM_cal': 0,
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ Per Øyvind Karlsen
Anton Kasyanov
Lou Kates
Makoto Kato
Irit Katriel
Hiroaki Kawai
Dmitry Kazakov
Brian Kearns
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed pprint's handling of dict subclasses that override __repr__.