Closed 16a7e08e-7e02-4841-a1bb-aca0d34587bd closed 10 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()"
Added py3 patch
py2
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.
A slightly more complete example that I tested:
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).
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
I consolidated the patches and example and have committed them to 2.7, 3.3 and 3.4. Thanks for your contributions.
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']
```