1919__author__ = 'Ka-Ping Yee'
2020__version__ = '$Revision$'
2121
22+ import sys
23+
2224def reset ():
2325 """Return a string that resets the CGI and browser to a known state."""
2426 return '''<!--: spam
@@ -66,7 +68,7 @@ def scanvars(reader, frame, locals):
6668
6769def html ((etype , evalue , etb ), context = 5 ):
6870 """Return a nice HTML document describing a given traceback."""
69- import sys , os , types , time , traceback , linecache , inspect , pydoc
71+ import os , types , time , traceback , linecache , inspect , pydoc
7072
7173 if type (etype ) is types .ClassType :
7274 etype = etype .__name__
@@ -149,18 +151,18 @@ def reader(lnum=[lnum]):
149151class Hook :
150152 """A hook to replace sys.excepthook that shows tracebacks in HTML."""
151153
152- def __init__ (self , display = 1 , logdir = None , context = 5 ):
154+ def __init__ (self , display = 1 , logdir = None , context = 5 , file = None ):
153155 self .display = display # send tracebacks to browser if true
154156 self .logdir = logdir # log tracebacks to files if not None
155157 self .context = context # number of source code lines per frame
158+ self .file = file or sys .stdout # place to send the output
156159
157160 def __call__ (self , etype , evalue , etb ):
158161 self .handle ((etype , evalue , etb ))
159162
160163 def handle (self , info = None ):
161- import sys
162164 info = info or sys .exc_info ()
163- print reset ()
165+ self . file . write ( reset () )
164166
165167 try :
166168 text , doc = 0 , html (info , self .context )
@@ -171,11 +173,11 @@ def handle(self, info=None):
171173 if self .display :
172174 if text :
173175 doc = doc .replace ('&' , '&' ).replace ('<' , '<' )
174- print '<pre>' + doc + '</pre>'
176+ self . file . write ( '<pre>' + doc + '</pre>\n ' )
175177 else :
176- print doc
178+ self . file . write ( doc + ' \n ' )
177179 else :
178- print '<p>A problem occurred in a Python script.'
180+ self . file . write ( '<p>A problem occurred in a Python script.\n ' )
179181
180182 if self .logdir is not None :
181183 import os , tempfile
@@ -185,9 +187,13 @@ def handle(self, info=None):
185187 file = open (path , 'w' )
186188 file .write (doc )
187189 file .close ()
188- print '<p> %s contains the description of this error.' % path
190+ msg = '<p> %s contains the description of this error.' % path
189191 except :
190- print '<p> Tried to save traceback to %s, but failed.' % path
192+ msg = '<p> Tried to save traceback to %s, but failed.' % path
193+ self .file .write (msg + '\n ' )
194+ try :
195+ self .file .flush ()
196+ except : pass
191197
192198handler = Hook ().handle
193199def enable (display = 1 , logdir = None , context = 5 ):
@@ -196,5 +202,4 @@ def enable(display=1, logdir=None, context=5):
196202 The optional argument 'display' can be set to 0 to suppress sending the
197203 traceback to the browser, and 'logdir' can be set to a directory to cause
198204 tracebacks to be written to files there."""
199- import sys
200205 sys .excepthook = Hook (display , logdir , context )
0 commit comments