Issue 1373762: Tweak pprint.PrettyPrinter.format for subclassing (original) (raw)
The current format() method doesn't recursively apply an overridden format() method when the width of the object is too short.
This patch is designed to remove that limitation and allow a pprint.PrettyPrinter sublcass that could, for example, print all ints and longs in hex:
class MyPrettyPrinter(pprint.PrettyPrinter): def format(self, object, context, maxlevels, level): if isinstance(object, int): return hex(object), True, False else: return pprintmod.PrettyPrinter.format( self, object, context, maxlevels, level)
mpp = MyPrettyPrinter() mpp.pprint(range(10)) [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9] mpp.pprint(range(0x10000000,0x10000010)) [0x10000000, 0x10000001, 0x10000002, 0x10000003, 0x10000004, 0x10000005, 0x10000006, 0x10000007, 0x10000008, 0x10000009, 0x1000000a, 0x1000000b, 0x1000000c, 0x1000000d, 0x1000000e, 0x1000000f]
The attached file contains "svn diff --diff-cmd diff - x -b Lib/pprint.py".
Logged In: YES user_id=3066
Heh, reading the diff alone doesn't help me; there's no helpful context.
While the example use case doesn't seem interesting, I consider non-support of sub-class overriding the format() method to be a bug.
I'll try and look at the patch more seriously soon, but I've not managed to eek out many tuits lately, and this week is as massively overbooked as any these days.