[New-bugs-announce] [issue29235] Allow profile/cProfile to be used as context managers

Thane Brimhall report at bugs.python.org
Tue Jan 10 20:13:40 EST 2017


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 <report at bugs.python.org>
<http://bugs.python.org/issue29235>
_______________________________________


More information about the New-bugs-announce mailing list