[New-bugs-announce] [issue28850] Regression in Python 3: Subclassing PrettyPrinter.format doesn't work anymore

Michael Enßlin report at bugs.python.org
Thu Dec 1 06:45:23 EST 2016


New submission from Michael Enßlin:

This issue was previously addressed and fixed here:

http://bugs.python.org/issue1351692

When subclassing PrettyPrinter, overriding the format() method should allow users to define custom pretty-printers.

However, for objects whose repr is short, format() is not called for the individual members.

Example code that reproduces the issue is as follows:


import pprint
import sys

pprint.pprint(sys.version_info)

class MyPrettyPrinter(pprint.PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if isinstance(object, int):
            return hex(object), True, False
        else:
            return pprint.PrettyPrinter.format(self, object, context, maxlevels, level)

MyPrettyPrinter().pprint(10)
MyPrettyPrinter().pprint([10])


When run with different versions of Python:


sys.version_info(major=2, minor=7, micro=11, releaselevel='final', serial=0)
0xa
[0xa]

(3, 0, 1, 'final', 0)
0xa
[10]

sys.version_info(major=3, minor=2, micro=5, releaselevel='final', serial=0)
0xa
[10]

sys.version_info(major=3, minor=4, micro=4, releaselevel='final', serial=0)
0xa
[10]

sys.version_info(major=3, minor=6, micro=0, releaselevel='beta', serial=4)
0xa
[10]

You'll notice that the regression exists in all versions >= 3.0,
even though the commit that fixed the issue is still in the log (2008-01-20):
https://hg.python.org/cpython/log/3.0/Lib/pprint.py

I have not had the time to look at the source of the issue or provide a fix; I might do so tonight.

----------
components: Library (Lib)
messages: 282159
nosy: anthonybaxter, doerwalter, georg.brandl, gvanrossum, markhirota, mic_e, rhettinger
priority: normal
severity: normal
status: open
title: Regression in Python 3: Subclassing PrettyPrinter.format doesn't work anymore
type: behavior
versions: Python 3.3, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28850>
_______________________________________


More information about the New-bugs-announce mailing list