classification
Title: Allow profile/cProfile to be used as context managers
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Scott Sanderson, Scott Sanderson2, Thane Brimhall, brett.cannon, cheryl.sabella, ethan.furman, inada.naoki, serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2017-01-11 01:13 by Thane Brimhall, last changed 2019-01-29 13:26 by cheryl.sabella. This issue is now closed.

Files
File name Uploaded Description Edit
profileContextManager.patch Thane Brimhall, 2017-01-16 03:35 review
Pull Requests
URL Status Linked Edit
PR 6808 merged python-dev, 2018-05-14 16:10
PR 7331 merged Scott Sanderson, 2018-06-01 22:27
Messages (10)
msg285175 - (view) Author: Thane Brimhall (Thane Brimhall) * Date: 2017-01-11 01:13
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()
msg285535 - (view) Author: Thane Brimhall (Thane Brimhall) * Date: 2017-01-16 03:35
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?
msg285536 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-01-16 03:40
> I suspect unit tests and documentation updates?

Yes. Code alone is not enough. It's better to have tests and documentation. Also, it's appreciated to sign the CLA: https://www.python.org/psf/contrib/contrib-form/. :-)
msg285538 - (view) Author: Thane Brimhall (Thane Brimhall) * Date: 2017-01-16 03:53
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.
msg316533 - (view) Author: Scott Sanderson (Scott Sanderson) * Date: 2018-05-14 16:19
This looks like it's been dormant for a bit. I've posted a patch (with tests and docs) to https://github.com/python/cpython/pull/6808.
msg318459 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-06-01 20:36
New changeset 2e01b75884892d5aabdaab658fbd17f7a7ccebaa by Brett Cannon (Scott Sanderson) in branch 'master':
bpo-29235: Make cProfile.Profile a context manager (GH-6808)
https://github.com/python/cpython/commit/2e01b75884892d5aabdaab658fbd17f7a7ccebaa
msg318460 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-01 20:42
Please add a versionadded or versionchanged directive in the documentation (not sure what is more appropriate in this case) and an entry in What's New.

A news entry shouldn't consist of several paragraphs. This will be lost when news entries will be merged into a single NEWS file.
msg318471 - (view) Author: Scott Sanderson (Scott Sanderson2) Date: 2018-06-01 22:29
I've posted a patch to update the docs to https://github.com/python/cpython/pull/7331.
msg318917 - (view) Author: Inada Naoki (inada.naoki) * (Python committer) Date: 2018-06-07 09:46
New changeset cebe80b59b7386db3cce904d280dab61d1037e7a by INADA Naoki (Scott Sanderson) in branch 'master':
bpo-29235: Update document for Profiler's context manager (GH-7331)
https://github.com/python/cpython/commit/cebe80b59b7386db3cce904d280dab61d1037e7a
msg334514 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-01-29 13:26
Closing as these changes have been merged for a few months.

msg285538 mentions that the pure-Python profiler doesn't have `enable` and `disable`, which is already opened in issue 32017.
History
Date User Action Args
2019-01-29 13:26:26cheryl.sabellasetstatus: open -> closed

nosy: + cheryl.sabella
messages: + msg334514

resolution: fixed
stage: patch review -> resolved
2018-06-07 09:46:45inada.naokisetnosy: + inada.naoki
messages: + msg318917
2018-06-01 22:29:25Scott Sanderson2setnosy: + Scott Sanderson2
messages: + msg318471
2018-06-01 22:27:21Scott Sandersonsetpull_requests: + pull_request6961
2018-06-01 20:42:58serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg318460
versions: + Python 3.8, - Python 3.7
2018-06-01 20:36:26brett.cannonsetnosy: + brett.cannon
messages: + msg318459
2018-05-14 16:19:48Scott Sandersonsetnosy: + Scott Sanderson
messages: + msg316533
2018-05-14 16:10:15python-devsetstage: test needed -> patch review
pull_requests: + pull_request6495
2017-01-16 03:53:46Thane Brimhallsetmessages: + msg285538
2017-01-16 03:40:42xiang.zhangsetnosy: + xiang.zhang

messages: + msg285536
stage: needs patch -> test needed
2017-01-16 03:35:34Thane Brimhallsetfiles: + profileContextManager.patch
keywords: + patch
messages: + msg285535
2017-01-11 03:52:29ethan.furmansetnosy: + ethan.furman

stage: needs patch
2017-01-11 01:13:40Thane Brimhallcreate