summaryrefslogtreecommitdiffstats
path: root/Lib/pprint.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-05-14 07:05:58 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-05-14 07:05:58 (GMT)
commita814db579db9cfcaa3cf2ca7bae539cf2c80a9f0 (patch)
tree6e96eb822db945a675a5e7a89a8ef3c22ba32150 /Lib/pprint.py
parent2a0af79269a19b4afc423c44f8b51c22fb8ad3a3 (diff)
downloadcpython-a814db579db9cfcaa3cf2ca7bae539cf2c80a9f0.zip
cpython-a814db579db9cfcaa3cf2ca7bae539cf2c80a9f0.tar.gz
cpython-a814db579db9cfcaa3cf2ca7bae539cf2c80a9f0.tar.bz2
SF bug[ #423781: pprint.isrecursive() broken.
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r--Lib/pprint.py50
1 files changed, 29 insertions, 21 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 814ca48..48efb33 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -49,26 +49,21 @@ def pprint(object, stream=None):
printer = PrettyPrinter(stream=stream)
printer.pprint(object)
-
def pformat(object):
"""Format a Python object into a pretty-printed representation."""
return PrettyPrinter().pformat(object)
+def saferepr(object):
+ """Version of repr() which can handle recursive data structures."""
+ return _safe_repr(object, {})[0]
def isreadable(object):
"""Determine if saferepr(object) is readable by eval()."""
- return PrettyPrinter().isreadable(object)
-
+ return _safe_repr(object, {})[1]
def isrecursive(object):
"""Determine if object requires a recursive representation."""
- return PrettyPrinter().isrecursive(object)
-
-
-def saferepr(object):
- """Version of repr() which can handle recursive data structures."""
- return _safe_repr(object, {})[0]
-
+ return _safe_repr(object, {})[2]
class PrettyPrinter:
def __init__(self, indent=1, width=80, depth=None, stream=None):
@@ -92,7 +87,7 @@ class PrettyPrinter:
indent = int(indent)
width = int(width)
assert indent >= 0
- assert (not depth) or depth > 0, "depth may not be negative"
+ assert depth is None or depth > 0, "depth must be > 0"
assert width
self.__depth = depth
self.__indent_per_level = indent
@@ -182,23 +177,28 @@ class PrettyPrinter:
del context[objid]
def __repr(self, object, context, level):
- repr, readable = _safe_repr(object, context, self.__depth, level)
+ repr, readable, recursive = _safe_repr(object, context,
+ self.__depth, level)
if not readable:
self.__readable = 0
+ if recursive:
+ self.__recursive = 1
return repr
+# Return triple (repr_string, isreadable, isrecursive).
def _safe_repr(object, context, maxlevels=None, level=0):
level = level + 1
typ = type(object)
if not (typ in (DictType, ListType, TupleType) and object):
rep = `object`
- return rep, (rep and (rep[0] != '<'))
+ return rep, (rep and (rep[0] != '<')), 0
if context.has_key(id(object)):
- return `_Recursion(object)`, 0
+ return `_Recursion(object)`, 0, 1
objid = id(object)
context[objid] = 1
readable = 1
+ recursive = 0
if typ is DictType:
if maxlevels and level >= maxlevels:
s = "{...}"
@@ -206,14 +206,20 @@ def _safe_repr(object, context, maxlevels=None, level=0):
else:
items = object.items()
k, v = items[0]
- krepr, kreadable = _safe_repr(k, context, maxlevels, level)
- vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
+ krepr, kreadable, krecur = _safe_repr(k, context, maxlevels,
+ level)
+ vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels,
+ level)
readable = readable and kreadable and vreadable
+ recursive = recursive or krecur or vrecur
s = "{%s: %s" % (krepr, vrepr)
for k, v in items[1:]:
- krepr, kreadable = _safe_repr(k, context, maxlevels, level)
- vrepr, vreadable = _safe_repr(v, context, maxlevels, level)
+ krepr, kreadable, krecur = _safe_repr(k, context, maxlevels,
+ level)
+ vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels,
+ level)
readable = readable and kreadable and vreadable
+ recursive = recursive or krecur or vrecur
s = "%s, %s: %s" % (s, krepr, vrepr)
s = s + "}"
else:
@@ -222,22 +228,24 @@ def _safe_repr(object, context, maxlevels=None, level=0):
s = s + "..."
readable = 0
else:
- subrepr, subreadable = _safe_repr(
+ subrepr, subreadable, subrecur = _safe_repr(
object[0], context, maxlevels, level)
readable = readable and subreadable
+ recursive = recursive or subrecur
s = s + subrepr
tail = object[1:]
if not tail:
if typ is TupleType:
s = s + ','
for ent in tail:
- subrepr, subreadable = _safe_repr(
+ subrepr, subreadable, subrecur = _safe_repr(
ent, context, maxlevels, level)
readable = readable and subreadable
+ recursive = recursive or subrecur
s = "%s, %s" % (s, subrepr)
s = s + term
del context[objid]
- return s, readable
+ return s, readable and not recursive, recursive
class _Recursion: