Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow profile/cProfile to be used as context managers #73421

Closed
ThaneBrimhall mannequin opened this issue Jan 11, 2017 · 10 comments
Closed

Allow profile/cProfile to be used as context managers #73421

ThaneBrimhall mannequin opened this issue Jan 11, 2017 · 10 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ThaneBrimhall
Copy link
Mannequin

ThaneBrimhall mannequin commented Jan 11, 2017

BPO 29235
Nosy @brettcannon, @methane, @ethanfurman, @serhiy-storchaka, @zhangyangyu, @ssanderson, @csabella
PRs
  • bpo-29235: Make cProfile.Profile a context manager. #6808
  • bpo-29235: Update document for Profiler's context manager #7331
  • Files
  • profileContextManager.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-01-29.13:26:26.543>
    created_at = <Date 2017-01-11.01:13:40.405>
    labels = ['3.8', 'type-feature', 'library']
    title = 'Allow profile/cProfile to be used as context managers'
    updated_at = <Date 2019-01-29.13:26:26.538>
    user = 'https://bugs.python.org/ThaneBrimhall'

    bugs.python.org fields:

    activity = <Date 2019-01-29.13:26:26.538>
    actor = 'cheryl.sabella'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-01-29.13:26:26.543>
    closer = 'cheryl.sabella'
    components = ['Library (Lib)']
    creation = <Date 2017-01-11.01:13:40.405>
    creator = 'Thane Brimhall'
    dependencies = []
    files = ['46295']
    hgrepos = []
    issue_num = 29235
    keywords = ['patch']
    message_count = 10.0
    messages = ['285175', '285535', '285536', '285538', '316533', '318459', '318460', '318471', '318917', '334514']
    nosy_count = 9.0
    nosy_names = ['brett.cannon', 'methane', 'ethan.furman', 'serhiy.storchaka', 'xiang.zhang', 'Scott Sanderson', 'Thane Brimhall', 'cheryl.sabella', 'Scott Sanderson2']
    pr_nums = ['6808', '7331']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29235'
    versions = ['Python 3.8']

    @ThaneBrimhall
    Copy link
    Mannequin Author

    ThaneBrimhall mannequin commented Jan 11, 2017

    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()

    @ThaneBrimhall ThaneBrimhall mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jan 11, 2017
    @ThaneBrimhall
    Copy link
    Mannequin Author

    ThaneBrimhall mannequin commented Jan 16, 2017

    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?

    @zhangyangyu
    Copy link
    Member

    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/. :-)

    @ThaneBrimhall
    Copy link
    Mannequin Author

    ThaneBrimhall mannequin commented Jan 16, 2017

    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.

    @ssanderson
    Copy link
    Mannequin

    ssanderson mannequin commented May 14, 2018

    This looks like it's been dormant for a bit. I've posted a patch (with tests and docs) to #6808.

    @brettcannon
    Copy link
    Member

    New changeset 2e01b75 by Brett Cannon (Scott Sanderson) in branch 'master':
    bpo-29235: Make cProfile.Profile a context manager (GH-6808)
    2e01b75

    @serhiy-storchaka
    Copy link
    Member

    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.

    @serhiy-storchaka serhiy-storchaka added 3.8 only security fixes and removed 3.7 (EOL) end of life labels Jun 1, 2018
    @ScottSanderson2
    Copy link
    Mannequin

    ScottSanderson2 mannequin commented Jun 1, 2018

    I've posted a patch to update the docs to #7331.

    @methane
    Copy link
    Member

    methane commented Jun 7, 2018

    New changeset cebe80b by INADA Naoki (Scott Sanderson) in branch 'master':
    bpo-29235: Update document for Profiler's context manager (GH-7331)
    cebe80b

    @csabella
    Copy link
    Contributor

    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 bpo-32017.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants