Index: Lib/pprint.py =================================================================== --- Lib/pprint.py (révision 79528) +++ Lib/pprint.py (copie de travail) @@ -38,6 +38,7 @@ import warnings from cStringIO import StringIO as _StringIO +from collections import defaultdict __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", "PrettyPrinter"] @@ -133,7 +134,7 @@ stream.write(_recursion(object)) self._recursive = True self._readable = False - return + return self._width rep = self._repr(object, context, level - 1) typ = _type(object) sepLines = _len(rep) > (self._width - 1 - indent - allowance) @@ -141,11 +142,15 @@ if self._depth and level > self._depth: write(rep) - return + return _len(rep) r = getattr(typ, "__repr__", None) - if issubclass(typ, dict) and r is dict.__repr__: - write('{') + if ((issubclass(typ, dict) and r is dict.__repr__) or + (issubclass(typ, defaultdict) and r is defaultdict.__repr__)): + if issubclass(typ, defaultdict): + write('defaultdict({') + else: + write('{') if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') length = _len(object) @@ -170,8 +175,11 @@ allowance + 1, context, level) indent = indent - self._indent_per_level del context[objid] - write('}') - return + if issubclass(typ, defaultdict): + write('})') + else: + write('}') + return self._width if ((issubclass(typ, list) and r is list.__repr__) or (issubclass(typ, tuple) and r is tuple.__repr__) or @@ -185,7 +193,7 @@ elif issubclass(typ, set): if not length: write('set()') - return + return self._width write('set([') endchar = '])' object = _sorted(object) @@ -193,7 +201,7 @@ elif issubclass(typ, frozenset): if not length: write('frozenset()') - return + return self._width write('frozenset([') endchar = '])' object = _sorted(object) @@ -206,24 +214,28 @@ if length: context[objid] = 1 indent = indent + self._indent_per_level - self._format(object[0], stream, indent, allowance + 1, + position = indent + position += self._format(object[0], stream, indent, allowance + 1, context, level) if length > 1: for ent in object[1:]: - if sepLines: + if sepLines and allowance + 1 > self._width - position: write(',\n' + ' '*indent) + position = (1 + indent) else: write(', ') - self._format(ent, stream, indent, - allowance + 1, context, level) + position += 2 + position += 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 + return self._width write(rep) + return _len(rep) def _repr(self, object, context, level): repr, readable, recursive = self.format(object, context.copy(),