python / cpython

The Python programming language
https://www.python.org
Other
61.98k stars 29.8k forks source link

Example for Profile Module shows incorrect method #62233

Closed 16a7e08e-7e02-4841-a1bb-aca0d34587bd closed 10 years ago

16a7e08e-7e02-4841-a1bb-aca0d34587bd commented 11 years ago
BPO 18033
Nosy @arigo, @orsenthil, @ezio-melotti, @phmc
Files
  • issue18033_py3.patch
  • issue18033_py2.patch
  • issue18033_py2.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: ```python assignee = None closed_at = created_at = labels = ['easy', 'type-feature', 'docs'] title = 'Example for Profile Module shows incorrect method' updated_at = user = 'https://bugs.python.org/jough' ``` bugs.python.org fields: ```python activity = actor = 'orsenthil' assignee = 'docs@python' closed = True closed_date = closer = 'orsenthil' components = ['Documentation'] creation = creator = 'jough' dependencies = [] files = ['30390', '30391', '30760'] hgrepos = [] issue_num = 18033 keywords = ['patch', 'easy'] message_count = 8.0 messages = ['189823', '190153', '190154', '190823', '190954', '192246', '197208', '197209'] nosy_count = 9.0 nosy_names = ['arigo', 'orsenthil', 'ezio.melotti', 'docs@python', 'python-dev', 'pconnell', 'dmi.baranov', 'jough', 'ohe'] pr_nums = [] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue18033' versions = ['Python 2.7', 'Python 3.3', 'Python 3.4'] ```

    16a7e08e-7e02-4841-a1bb-aca0d34587bd commented 11 years ago

    The example on this page: http://docs.python.org/2/library/profile.html?highlight=pstats#profile.Profile

    Shows:

    import cProfile, pstats, io
    pr = cProfile.Profile()
    pr.enable()
    ... do something ...
    pr.disable()
    s = io.StringIO()
    ps = pstats.Stats(pr, stream=s)
    ps.print_results()

    Where "ps.print_results()" should be "ps.print_stats()"

    f6356695-ef8f-41ef-8e4e-18b74f95dfdd commented 11 years ago

    Added py3 patch

    f6356695-ef8f-41ef-8e4e-18b74f95dfdd commented 11 years ago

    py2

    ezio-melotti commented 11 years ago

    Thanks for the patches, however the Python 2 example doesn't work. I think a BytesIO should be used instead of a StringIO, and print_stats() only returns a pstats.Stats instance, without actually printing any result. I wonder if print_results was an old method that has been removed and if now there's another way to print the results. I haven't tried on Python 3 yet, but the same comment might apply there too.

    7aa6e20b-8983-474f-b2ae-de7eff1caa04 commented 11 years ago

    A slightly more complete example that I tested:

    http://stackoverflow.com/a/16077568/1556290

    aed7a3f8-3148-4ba8-b568-713e1a47829b commented 11 years ago

    Here is a fixed version for python2.7. Using StringIO instead of io module fixes the problem pointed by Ezio. The print_stats method dumps the stats either on sys.stdout if Stats class is declared without a stream specification or in the given stream parameter (the name print_stats may be a bit misleading).

    1762cc99-3127-4a62-9baf-30c3d0f51ef7 commented 10 years ago

    New changeset 93018d47793f by Senthil Kumaran in branch '2.7': Correct Profile class usage example. Addresses issue bpo-18033 . http://hg.python.org/cpython/rev/93018d47793f

    New changeset ab4d3ccb92e6 by Senthil Kumaran in branch '3.3': Correct Profile class usage example. Addresses issue bpo-18033. http://hg.python.org/cpython/rev/ab4d3ccb92e6

    New changeset 88182b388bae by Senthil Kumaran in branch 'default': merge from 3.3 http://hg.python.org/cpython/rev/88182b388bae

    orsenthil commented 10 years ago

    I consolidated the patches and example and have committed them to 2.7, 3.3 and 3.4. Thanks for your contributions.