[New-bugs-announce] [issue29238] Add more kwargs to cProfile's print_stats

Thane Brimhall report at bugs.python.org
Tue Jan 10 22:00:31 EST 2017


New submission from Thane Brimhall:

Using the pstats class to print off profiler results is helpful when you want to filter, order, and limit the number of returned lines. I'd rather see the most commonly-used subset of pstats functionality included in the profiler's print_stats implementation directly.


    # The current way
    pstats.Stats(profiler).strip_dirs().sort_stats('cumulative').reverse_order().print_stats(10)

    # Proposed way
    profiler.print_stats(strip_dirs=False, sort='cumulative', reverse=True, limit=10)


Currently only the `sort` kwarg is available. To me this implies that some level of control was originally intended to be available in the print_stats method anyway. I also feel like the proposed API is more readable and explicit.

Note that for complex situations you'd still need to use the pstats class directly, eg. substituting a different stream implementation or filtering/sorting by multiple values.

This would be a backwards-compatible patch and would be implemented something like this:


    def print_stats(self, sort=-1, limit=None, strip_dirs=True, reverse=True):
        import pstats
        stats = pstats.Stats(self)
        if strip_dirs:
            stats = stats.strip_dirs()
        stats = stats.sort_stats(sort)
        if reverse:
            stats = stats.reverse_order()
        stats.print_stats(limit)

----------
components: Library (Lib)
messages: 285183
nosy: Thane Brimhall
priority: normal
severity: normal
status: open
title: Add more kwargs to cProfile's print_stats
type: enhancement
versions: Python 3.7

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


More information about the New-bugs-announce mailing list