#1351692: in pprint, always call format() for dict and list items to enable
custom formatting of contents via subclassing PrettyPrinter.
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 19a3dc2..89f99d2 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -131,65 +131,70 @@
sepLines = _len(rep) > (self._width - 1 - indent - allowance)
write = stream.write
- if sepLines:
- r = getattr(typ, "__repr__", None)
- if issubclass(typ, dict) and r is dict.__repr__:
- write('{')
- if self._indent_per_level > 1:
- write((self._indent_per_level - 1) * ' ')
- length = _len(object)
- if length:
- context[objid] = 1
- indent = indent + self._indent_per_level
- items = object.items()
- items.sort()
- key, ent = items[0]
- rep = self._repr(key, context, level)
- write(rep)
- write(': ')
- self._format(ent, stream, indent + _len(rep) + 2,
- allowance + 1, context, level)
- if length > 1:
- for key, ent in items[1:]:
- rep = self._repr(key, context, level)
+ r = getattr(typ, "__repr__", None)
+ if issubclass(typ, dict) and r is dict.__repr__:
+ write('{')
+ if self._indent_per_level > 1:
+ write((self._indent_per_level - 1) * ' ')
+ length = _len(object)
+ if length:
+ context[objid] = 1
+ indent = indent + self._indent_per_level
+ items = object.items()
+ items.sort()
+ key, ent = items[0]
+ rep = self._repr(key, context, level)
+ write(rep)
+ write(': ')
+ self._format(ent, stream, indent + _len(rep) + 2,
+ allowance + 1, context, level)
+ if length > 1:
+ for key, ent in items[1:]:
+ rep = self._repr(key, context, level)
+ if sepLines:
write(',\n%s%s: ' % (' '*indent, rep))
- self._format(ent, stream, indent + _len(rep) + 2,
- allowance + 1, context, level)
- indent = indent - self._indent_per_level
- del context[objid]
- write('}')
- return
+ else:
+ write(', %s: ' % rep)
+ self._format(ent, stream, indent + _len(rep) + 2,
+ allowance + 1, context, level)
+ indent = indent - self._indent_per_level
+ del context[objid]
+ write('}')
+ return
- if (issubclass(typ, list) and r is list.__repr__) or \
- (issubclass(typ, tuple) and r is tuple.__repr__):
- if issubclass(typ, list):
- write('[')
- endchar = ']'
- else:
- write('(')
- endchar = ')'
- if self._indent_per_level > 1:
- write((self._indent_per_level - 1) * ' ')
- length = _len(object)
- if length:
- context[objid] = 1
- indent = indent + self._indent_per_level
- self._format(object[0], stream, indent, allowance + 1,
- context, level)
- if length > 1:
- for ent in object[1:]:
+ if (issubclass(typ, list) and r is list.__repr__) or \
+ (issubclass(typ, tuple) and r is tuple.__repr__):
+ if issubclass(typ, list):
+ write('[')
+ endchar = ']'
+ else:
+ write('(')
+ endchar = ')'
+ if self._indent_per_level > 1:
+ write((self._indent_per_level - 1) * ' ')
+ length = _len(object)
+ if length:
+ context[objid] = 1
+ indent = indent + self._indent_per_level
+ self._format(object[0], stream, indent, allowance + 1,
+ context, level)
+ if length > 1:
+ for ent in object[1:]:
+ if sepLines:
write(',\n' + ' '*indent)
- self._format(ent, stream, indent,
- allowance + 1, context, level)
- indent = indent - self._indent_per_level
- del context[objid]
- if issubclass(typ, tuple) and length == 1:
- write(',')
- write(endchar)
- return
-
+ else:
+ write(', ')
+ self._format(ent, stream, indent,
+ allowance + 1, context, level)
+ indent = indent - self._indent_per_level
+ del context[objid]
+ if issubclass(typ, tuple) and length == 1:
+ write(',')
+ write(endchar)
+ return
write(rep)
+
def _repr(self, object, context, level):
repr, readable, recursive = self.format(object, context.copy(),
self._depth, level)