web.profiler has unexpected behavior in python 2.5
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
web.py |
Fix Released
|
Medium
|
Anand Chitipothu |
Bug Description
pstats.py was changed either in python 2.4 or 2.5 to print out to it's own self.stream which is pointed at sys.stdout when the object is first initialized. This break the capturestdout class with web.profiler. The sys.stdout needs to be redirected before the object is created:
class Profile:
"""
Profiles `func` and returns a tuple containing its output
and a string with human-readable profiling information.
>>> import time
>>> out, inf = profile(
>>> out
>>> inf[:10].strip()
'took 0.0'
"""
def __init__(self, func):
self.func = func
def __call__(self, *args): ##, **kw): kw unused
import hotshot, hotshot.stats, tempfile ##, time already imported
temp = tempfile.
prof = hotshot.
stime = time.time()
result = prof.runcall(
stime = time.time() - stime
from cStringIO import StringIO
# Not threadsafe!
out = StringIO()
oldstdout = sys.stdout
sys.stdout = out
try:
x = '\n\ntook '+ str(stime) + ' seconds\n'
x += out.getvalue()
finally:
return result, x
description: | updated |
Changed in webpy: | |
assignee: | nobody → anandology |
importance: | Undecided → Medium |
status: | New → Confirmed |
Changed in webpy: | |
milestone: | none → 0.23 |
status: | Confirmed → Fix Committed |
Changed in webpy: | |
status: | Fix Committed → Fix Released |
The above patch does the work of capturestdout also. Here is much cleaner fix.
def print_stats(): stats.load( temp.name) strip_dirs( ) sort_stats( 'time', 'calls') print_stats( 40) print_callers( )
stats = hotshot.
stats.
stats.
stats.
stats.
x = '\n\ntook '+ str(stime) + ' seconds\n' print_stats) ()
x += capturestdout(