[issue29235] Allow profile/cProfile to be used as context managers
Thane Brimhall added the comment: I've signed the agreement. I will do the necessary research to figure out how to do unit tests and documentation updates. I should also mention that while maintaining API-compatibility with `profile` was a stated goal, it turns out that the pure-python profiler actually does not have the enable() and disable() methods that the context manager needs. -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29235> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29235] Allow profile/cProfile to be used as context managers
Thane Brimhall added the comment: So this is my first time contributing to Python, but here's a (trivial) patch for this issue. Let me know what else is required to make this happen. I suspect unit tests and documentation updates? -- keywords: +patch Added file: http://bugs.python.org/file46295/profileContextManager.patch ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29235> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29238] Add more kwargs to cProfile's print_stats
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 <rep...@bugs.python.org> <http://bugs.python.org/issue29238> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29237] Create enum for pstats sorting options
New submission from Thane Brimhall: When using the cProfile and pstats modules, I often forget which string keys are available for sorting. It would be nice to add an enum for these so a user could have their linter and IDE check that value pre-runtime. By subclassing both `str` and `Enum` this proposal would be backwards-compatible with all existing code. The patch for such a change would be trivial: 1. Add a new Sort class to the pstats module: class Sort(str, enum.Enum): calls = 'calls' # call count cumulative = 'cumulative' # cumulative time cumtime = 'cumtime' # cumulative time file = 'file' # file name filename = 'filename' # file name module = 'module' # file name ncalls = 'ncalls' # call count pcalls = 'pcalls' # primitive call count line = 'line' # line number name = 'name' # function name nfl = 'nfl' # name/file/line stdname = 'stdname' # standard name time = 'time' # internal time tottime = 'tottime' # internal time 2. Change the print_stats method signature on the profiler base and subclasses to look like this: def print_stats(self, sort: Sort=Sort.stdname): Optionally, you could build the Sort enum like below to remove redundant options and increase explicitness: class Sort(str, enum.Enum): call_count = 'calls' cumulative_time = 'cumulative' filename = 'filename' primitive_call_count = 'pcalls' line_number = 'line' function_name = 'name' name_file_line = 'nfl' standard_name = 'stdname' internal_time = 'time' -- components: Library (Lib) messages: 285178 nosy: Thane Brimhall priority: normal severity: normal status: open title: Create enum for pstats sorting options type: enhancement versions: Python 3.7 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29237> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29235] Allow profile/cProfile to be used as context managers
New submission from Thane Brimhall: The `enable` and `disable` methods on the profilers fit the description of a context manager very well. The following code: pr = cProfile.Profile() pr.enable() # ... do something ... pr.disable() pr.print_stats() Would turn into something like this: with cProfile.Profile() as pr: # ... do something ... pr.print_stats() The patch for this code would be trivial and backwards-compatible: simply add something like the following lines to the _lsprof.Profiler base class: def __enter__(self): self.enable() return self def __exit__(self, exc_type, exc_val, exc_tb): self.disable() -- components: Library (Lib) messages: 285175 nosy: Thane Brimhall priority: normal severity: normal status: open title: Allow profile/cProfile to be used as context managers type: enhancement versions: Python 3.7 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29235> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com