diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -932,10 +932,11 @@ class hgweb(object): try: ret = self.repo.addchangegroup(fp, 'serve') - req.write('%d\n' % ret) - req.write(sys.stdout.getvalue()) finally: + val = sys.stdout.getvalue() sys.stdout = old_stdout + req.write('%d\n' % ret) + req.write(val) finally: lock.release() finally: diff --git a/mercurial/hgweb/wsgicgi.py b/mercurial/hgweb/wsgicgi.py --- a/mercurial/hgweb/wsgicgi.py +++ b/mercurial/hgweb/wsgicgi.py @@ -27,6 +27,7 @@ def launch(application): headers_set = [] headers_sent = [] + out = sys.stdout def write(data): if not headers_set: @@ -35,13 +36,13 @@ def launch(application): elif not headers_sent: # Before the first output, send the stored headers status, response_headers = headers_sent[:] = headers_set - sys.stdout.write('Status: %s\r\n' % status) + out.write('Status: %s\r\n' % status) for header in response_headers: - sys.stdout.write('%s: %s\r\n' % header) - sys.stdout.write('\r\n') + out.write('%s: %s\r\n' % header) + out.write('\r\n') - sys.stdout.write(data) - sys.stdout.flush() + out.write(data) + out.flush() def start_response(status,response_headers,exc_info=None): if exc_info: