summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-05-12 16:26:52 +0000
committerGeorg Brandl <georg@python.org>2008-05-12 16:26:52 +0000
commit23da6e654586bd59af566c6ed5d3e89bc55e8b23 (patch)
treee5e5f089a061e30920217a4f61f4e16827f372c2
parent103f19d286d7b0a80bd83031d8fd1c0af31dfd9e (diff)
downloadcpython-git-23da6e654586bd59af566c6ed5d3e89bc55e8b23.tar.gz
#1713041: fix pprint's handling of maximum depth.
-rw-r--r--Doc/library/pprint.rst5
-rw-r--r--Lib/pprint.py10
-rw-r--r--Lib/test/test_pprint.py15
-rw-r--r--Misc/NEWS2
4 files changed, 26 insertions, 6 deletions
diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst
index b3778f13d9..c0bedf5d5b 100644
--- a/Doc/library/pprint.rst
+++ b/Doc/library/pprint.rst
@@ -66,8 +66,7 @@ The :mod:`pprint` module defines one class:
... ('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 @@ This example demonstrates several uses of the :func:`pprint` function and its pa
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, depth=3)
['aaaaaaaaaa',
- ('spam', ('eggs', ('lumberjack', (...)))),
+ ('spam', ('eggs', (...))),
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, width=60)
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 9f9d6a216c..93d850aca1 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -131,6 +131,10 @@ class PrettyPrinter:
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 @@ class PrettyPrinter:
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 @@ def _safe_repr(object, context, maxlevels, level):
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 @@ def _safe_repr(object, context, maxlevels, level):
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
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index 83c772b5b0..4d7a3ed147 100644
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -387,6 +387,21 @@ class QueryTestCase(unittest.TestCase):
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):
diff --git a/Misc/NEWS b/Misc/NEWS
index 06478338f0..4431d904ec 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,8 @@ Extension Modules
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.