diff options
| author | ianb <devnull@localhost> | 2006-12-01 19:20:53 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2006-12-01 19:20:53 +0000 |
| commit | 46343c9ca6e82d9623997077bb1c21aecb6e0b4e (patch) | |
| tree | b9a7d58ade72ff7158331cd9b2010a54992a0288 /paste/debug | |
| parent | a15b6e7e8e08f6ab8e3299be33fea6aa6771acfd (diff) | |
| download | paste-46343c9ca6e82d9623997077bb1c21aecb6e0b4e.tar.gz | |
Remove sys.stdout patching, in favor of directly capturing output
Diffstat (limited to 'paste/debug')
| -rw-r--r-- | paste/debug/profile.py | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/paste/debug/profile.py b/paste/debug/profile.py index cf4f085..ada4d86 100644 --- a/paste/debug/profile.py +++ b/paste/debug/profile.py @@ -66,30 +66,21 @@ 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') - 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)) + stats.print_stats(self.limit) + stats.print_callers(self.limit) + output = output.getvalue() + body += '<pre style="%s">%s</pre>' % ( + self.style, cgi.escape(output)) 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): """ @@ -168,14 +159,15 @@ 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) - output = capture_output(stats.print_stats, display_limit) - output_callers = capture_output( - stats.print_callers, display_limit) + stats.print_stats(display_limit) + stats.print_callers(display_limit) output_file = ops.get('log_file') if output_file in (None, 'stderr'): f = sys.stderr @@ -189,8 +181,7 @@ class DecoratedProfile(object): % self.format_function(func, *args, **kw)) f.write('Wall time: %0.2f seconds\n' % (end_time - start_time)) - f.write(output) - f.write(output_callers) + f.write(output.getvalue()) if output_file not in (None, '-', 'stdout', 'stderr'): f.close() if exc_info: |
