This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mic_e
Recipients anthonybaxter, doerwalter, georg.brandl, gvanrossum, markhirota, mic_e, rhettinger
Date 2016-12-01.11:45:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1480592723.97.0.369129599105.issue28850@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-12-01 11:45:24mic_esetrecipients: + mic_e, gvanrossum, doerwalter, anthonybaxter, georg.brandl, rhettinger, markhirota
2016-12-01 11:45:23mic_esetmessageid: <1480592723.97.0.369129599105.issue28850@psf.upfronthosting.co.za>
2016-12-01 11:45:23mic_elinkissue28850 messages
2016-12-01 11:45:23mic_ecreate