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

Reply via email to