diff options
| author | ianb <devnull@localhost> | 2006-12-18 00:36:27 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2006-12-18 00:36:27 +0000 |
| commit | 4be16aa6ceb754e938525a9c377d574fede0a12a (patch) | |
| tree | d162f3544b28e2f098598ecbf2ec9d8678e15a71 /paste/debug | |
| parent | 8fd237055f44f36241c1cd7bf240c7ffa189ee1e (diff) | |
| download | paste-4be16aa6ceb754e938525a9c377d574fede0a12a.tar.gz | |
Revert r5958, for more threadsafe profiling -- the feature it used is only available in Python 2.5
Diffstat (limited to 'paste/debug')
| -rw-r--r-- | paste/debug/profile.py | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/paste/debug/profile.py b/paste/debug/profile.py index ada4d86..cf4f085 100644 --- a/paste/debug/profile.py +++ b/paste/debug/profile.py @@ -66,21 +66,30 @@ class ProfileMiddleware(object): if not content_type.startswith('text/html'): # We can't add info to non-HTML output return [body] - output = StringIO() stats = hotshot.stats.load(self.log_filename) - # Makes output go to this stream: - stats.stream = output stats.strip_dirs() stats.sort_stats('time', 'calls') - stats.print_stats(self.limit) - stats.print_callers(self.limit) - output = output.getvalue() - body += '<pre style="%s">%s</pre>' % ( - self.style, cgi.escape(output)) + output = capture_output(stats.print_stats, self.limit) + output_callers = capture_output( + stats.print_callers, self.limit) + body += '<pre style="%s">%s\n%s</pre>' % ( + self.style, cgi.escape(output), cgi.escape(output_callers)) return [body] finally: self.lock.release() +def capture_output(func, *args, **kw): + # Not threadsafe! (that's okay when ProfileMiddleware uses it, + # though, since it synchronizes itself.) + out = StringIO() + old_stdout = sys.stdout + sys.stdout = out + try: + func(*args, **kw) + finally: + sys.stdout = old_stdout + return out.getvalue() + def profile_decorator(**options): """ @@ -159,15 +168,14 @@ class DecoratedProfile(object): finally: prof.close() stats = hotshot.stats.load(prof_filename) - output = StringIO() - stats.stream = output os.unlink(prof_filename) if ops.get('strip_dirs', True): stats.strip_dirs() stats.sort_stats(*ops.get('sort_stats', ('time', 'calls'))) display_limit = ops.get('display_limit', 20) - stats.print_stats(display_limit) - stats.print_callers(display_limit) + output = capture_output(stats.print_stats, display_limit) + output_callers = capture_output( + stats.print_callers, display_limit) output_file = ops.get('log_file') if output_file in (None, 'stderr'): f = sys.stderr @@ -181,7 +189,8 @@ class DecoratedProfile(object): % self.format_function(func, *args, **kw)) f.write('Wall time: %0.2f seconds\n' % (end_time - start_time)) - f.write(output.getvalue()) + f.write(output) + f.write(output_callers) if output_file not in (None, '-', 'stdout', 'stderr'): f.close() if exc_info: |
