Skip to content

Commit

Permalink
Merge pull request #2092 from bfroehle/prun_print_stats
Browse files Browse the repository at this point in the history
%prun: Restore `stats.stream` after running `print_stream`

When profiling, the output from `print_stream` is captured so that the output can be paged. To do so, the `stream` attribute is set to a `StringIO` object but was not restored after the output was captured. As a result, the stats object returned by `%prun -r` did not display any output when calling its `print_stream` method.

In addition, the `pstats.Stats` class has the `stream` attribute for all currently supported Python versions (2.6, 2.7, and 3.2, at least), so the extra branch of code can be removed.

Closes #2091
  • Loading branch information
minrk committed Jul 21, 2012
2 parents aa922b1 + cb56d19 commit 965fc65
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions IPython/core/magics/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,12 @@ def prun(self, parameter_s='', cell=None, user_mode=True,

# Trap output.
stdout_trap = StringIO()

if hasattr(stats,'stream'):
# In newer versions of python, the stats object has a 'stream'
# attribute to write into.
stats_stream = stats.stream
try:
stats.stream = stdout_trap
stats.print_stats(*lims)
else:
# For older versions, we manually redirect stdout during printing
sys_stdout = sys.stdout
try:
sys.stdout = stdout_trap
stats.print_stats(*lims)
finally:
sys.stdout = sys_stdout
finally:
stats.stream = stats_stream

output = stdout_trap.getvalue()
output = output.rstrip()
Expand Down

0 comments on commit 965fc65

Please sign in to comment.