From f39d0516b44c80fbcdbd7f8058bc2716224c127f Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Wed, 16 Apr 1997 18:55:58 +0000 Subject: _safe_repr(): Make the context parameter required; ok since it's only for internal use. Make sure the maxlevels and level parameters get passed to recursive invocations. --- Lib/pprint.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Lib/pprint.py b/Lib/pprint.py index 850b0f7..a16edb3 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -153,16 +153,13 @@ class PrettyPrinter: return _safe_repr(object, context, self.__depth, level) -def _safe_repr(object, context=None, maxlevels=None, level=0): +def _safe_repr(object, context, maxlevels=None, level=0): level = level + 1 typ = type(object) if not (typ in (DictType, ListType, TupleType) and object): return `object` - if context is None: - context = {} - else: - if context.has_key(id(object)): - return `_Recursion(object)` + if context.has_key(id(object)): + return `_Recursion(object)` objid = id(object) context[objid] = 1 if typ is DictType: @@ -171,19 +168,21 @@ def _safe_repr(object, context=None, maxlevels=None, level=0): else: items = object.items() k, v = items[0] - s = "{%s: %s" % (_safe_repr(k, context), _safe_repr(v, context)) + s = "{%s: %s" % (_safe_repr(k, context, maxlevels, level), + _safe_repr(v, context, maxlevels, level)) for k, v in items[1:]: s = "%s, %s: %s" \ - % (s, _safe_repr(k, context), _safe_repr(v, context)) + % (s, _safe_repr(k, context, maxlevels, level), + _safe_repr(v, context, maxlevels, level)) s = s + "}" else: s, term = (typ is ListType) and ('[', ']') or ('(', ')') if maxlevels and level >= maxlevels: s = s + "..." else: - s = s + _safe_repr(object[0], context) + s = s + _safe_repr(object[0], context, maxlevels, level) for ent in object[1:]: - s = "%s, %s" % (s, _safe_repr(ent, context)) + s = "%s, %s" % (s, _safe_repr(ent, context, maxlevels, level)) s = s + term del context[objid] return s -- cgit v0.12