[Python-checkins] r63164 - in python/trunk: Doc/library/pprint.rst Lib/pprint.py Lib/test/test_pprint.py Misc/NEWS
georg.brandl
python-checkins at python.org
Mon May 12 18:26:53 CEST 2008
Author: georg.brandl
Date: Mon May 12 18:26:52 2008
New Revision: 63164
Log:
#1713041: fix pprint's handling of maximum depth.
Modified:
python/trunk/Doc/library/pprint.rst
python/trunk/Lib/pprint.py
python/trunk/Lib/test/test_pprint.py
python/trunk/Misc/NEWS
Modified: python/trunk/Doc/library/pprint.rst
==============================================================================
--- python/trunk/Doc/library/pprint.rst (original)
+++ python/trunk/Doc/library/pprint.rst Mon May 12 18:26:52 2008
@@ -66,8 +66,7 @@
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)
- ('spam',
- ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', (...,))))))))
+ ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
The :class:`PrettyPrinter` class supports several derivative functions:
@@ -220,7 +219,7 @@
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, depth=3)
['aaaaaaaaaa',
- ('spam', ('eggs', ('lumberjack', (...)))),
+ ('spam', ('eggs', (...))),
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, width=60)
Modified: python/trunk/Lib/pprint.py
==============================================================================
--- python/trunk/Lib/pprint.py (original)
+++ python/trunk/Lib/pprint.py Mon May 12 18:26:52 2008
@@ -131,6 +131,10 @@
sepLines = _len(rep) > (self._width - 1 - indent - allowance)
write = stream.write
+ if self._depth and level > self._depth:
+ write(rep)
+ return
+
r = getattr(typ, "__repr__", None)
if issubclass(typ, dict) and r is dict.__repr__:
write('{')
@@ -211,8 +215,8 @@
write(',')
write(endchar)
return
- write(rep)
+ write(rep)
def _repr(self, object, context, level):
repr, readable, recursive = self.format(object, context.copy(),
@@ -259,7 +263,7 @@
if not object:
return "{}", True, False
objid = _id(object)
- if maxlevels and level > maxlevels:
+ if maxlevels and level >= maxlevels:
return "{...}", False, objid in context
if objid in context:
return _recursion(object), False, True
@@ -293,7 +297,7 @@
return "()", True, False
format = "(%s)"
objid = _id(object)
- if maxlevels and level > maxlevels:
+ if maxlevels and level >= maxlevels:
return format % "...", False, objid in context
if objid in context:
return _recursion(object), False, True
Modified: python/trunk/Lib/test/test_pprint.py
==============================================================================
--- python/trunk/Lib/test/test_pprint.py (original)
+++ python/trunk/Lib/test/test_pprint.py Mon May 12 18:26:52 2008
@@ -387,6 +387,21 @@
cubo = test.test_set.linegraph(cube)
self.assertEqual(pprint.pformat(cubo), cubo_repr_tgt)
+ def test_depth(self):
+ nested_tuple = (1, (2, (3, (4, (5, 6)))))
+ nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
+ nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
+ self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
+ self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
+ self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
+
+ lv1_tuple = '(1, (...))'
+ lv1_dict = '{1: {...}}'
+ lv1_list = '[1, [...]]'
+ self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
+ self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
+ self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
+
class DottedPrettyPrinter(pprint.PrettyPrinter):
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS (original)
+++ python/trunk/Misc/NEWS Mon May 12 18:26:52 2008
@@ -26,6 +26,8 @@
Library
-------
+- #1713041: fix pprint's handling of maximum depth.
+
- The timing module has been deprecated for removal in Python 3.0.
- The sv module has been deprecated for removal in Python 3.0.
More information about the Python-checkins
mailing list