[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()
    # ... do something ...

Would turn into something like this:

    with cProfile.Profile() as pr:
        # ... do something ...

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):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):

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>

More information about the New-bugs-announce mailing list