pstats.Stats objects should allow sorting caller/callee stats

This is an annoyance I’ve run into multiple times over many years while profiling python code and yesterday when I hit it again I also found another who encountered this problem: python - Why is pstats.print_callers() ignores the restriction argument? - Stack Overflow

Simply put: print_callers(*amount) only applies its provided filters to the top-level details (the callees for print_callers and callers for print_callees), and the second-level details are unfiltered and left sorted by name. If you want to print the top 20 callers of a function with 1000 callers and you run print_callers(funcname, 20), you will get the top 20 matches of funcname and each will have all 1000 callers. Hence, the nominal workflow is to print the results, copy them or read them back in, parse them by splitting on whitespace and casting to ints and floats, and then sort.

I’m proposing adding keyword arguments to print_callers and print_callees to allow sorting and filtering of the second-level details. Implementation doesn’t look too difficult since there’s already similar code that would be shared with sort_stats and get_print_line and mainly would touch print_call_line; I’m about to attempt it myself.

2 Likes

This seems to be a worth while change.

Once you have an implementation it might be worth seeing if python devs will accept it as a PR.

I’ve got an implementation and briefly tested it out (though I did not see a way to run IDLE and the console repl was not great around arrow keys). I’ll be filing an issue and PR linking here, if anyone else would like to chime in here or like this discussion as per the criteria for “widespread approval”. :slight_smile:

Why is this important? Does not seem to be related to a pstat change.

Not related, no, just would have made it easier to test in a more familiar environment.

For testing i write shell scripts often so that the tests are repeatable.