mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Add an experimental mechanism to support extending the pprint formatting.
Partly responds to SF bug #505152.
This commit is contained in:
parent
4993c51b94
commit
aee113d368
3 changed files with 57 additions and 9 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue