summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2002-04-02 05:08:35 (GMT)
committerFred Drake <fdrake@acm.org>2002-04-02 05:08:35 (GMT)
commitaee113d368ebad8a51386baa7c0aec056aca94d5 (patch)
tree9a81e154b2772284d823cd0df3f966faa59a93e2 /Lib
parent4993c51b9436bc28126659519c362558fdcdd0d7 (diff)
downloadcpython-aee113d368ebad8a51386baa7c0aec056aca94d5.zip
cpython-aee113d368ebad8a51386baa7c0aec056aca94d5.tar.gz
cpython-aee113d368ebad8a51386baa7c0aec056aca94d5.tar.bz2
Add an experimental mechanism to support extending the pprint formatting.
Partly responds to SF bug #505152.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pprint.py22
-rw-r--r--Lib/test/test_pprint.py21
2 files changed, 34 insertions, 9 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py
index 5d178a2..82eeac6 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -115,15 +115,11 @@ class PrettyPrinter:
return sio.getvalue()
def isrecursive(self, object):
- self.__recursive = 0
- self.__repr(object, {}, 0)
- return self.__recursive
+ return self.format(object, {}, 0)[2]
def isreadable(self, object):
- self.__recursive = 0
- self.__readable = 1
- self.__repr(object, {}, 0)
- return self.__readable and not self.__recursive
+ s, readable, recursive = self.format(object, {}, 0)
+ return readable and not recursive
def __format(self, object, stream, indent, allowance, context, level):
level = level + 1
@@ -196,14 +192,22 @@ class PrettyPrinter:
write(rep)
def __repr(self, object, context, level):
- repr, readable, recursive = _safe_repr(object, context,
- self.__depth, level)
+ repr, readable, recursive = self.format(object, context.copy(),
+ self.__depth, level)
if not readable:
self.__readable = 0
if recursive:
self.__recursive = 1
return repr
+ def format(self, object, context, maxlevels, level):
+ """Format object for a specific context, returning a string
+ and flags indicating whether the representation is 'readable'
+ and whether the object represents a recursive construct.
+ """
+ return _safe_repr(object, context, maxlevels, level)
+
+
# Return triple (repr_string, isreadable, isrecursive).
def _safe_repr(object, context, maxlevels, level):
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index 14626fb..d5e10aa 100644
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -96,6 +96,27 @@ class QueryTestCase(unittest.TestCase):
'write_io_runtime_us': 43690}"""
self.assertEqual(pprint.pformat(o), exp)
+ def test_subclassing(self):
+ o = {'names with spaces': 'should be presented using repr()',
+ 'others.should.not.be': 'like.this'}
+ exp = """\
+{'names with spaces': 'should be presented using repr()',
+ others.should.not.be: like.this}"""
+ self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
+
+
+class DottedPrettyPrinter(pprint.PrettyPrinter):
+ def format(self, object, context, maxlevels, level):
+ if isinstance(object, str):
+ if ' ' in object:
+ return `object`, 1, 0
+ else:
+ return object, 0, 0
+ else:
+ return pprint.PrettyPrinter.format(
+ self, object, context, maxlevels, level)
+
+
def test_main():
test_support.run_unittest(QueryTestCase)