Possible to capture cgitb style output in a try/except section?

Malcolm Greene python at bdurham.com
Tue Jul 26 11:52:47 EDT 2016


Hi Steven and Peter,

Steven: Interestingly (oddly???) enough, the output captured by hooking
the cgitb handler on my system appears to be shorter than the default
cgitb output. You can see this yourself via this tiny script:

import cgitb
cgitb.enable(format='text')
x = 1/0

The solution I came up with (Python 3.5.1) was to use your StringIO
technique with the Hook's 'file=' parameter.

import io

cgitb_buffer = io.StringIO()
cgitb.Hook(file=cgitb_buffer, format='text)
return cgitb_buffer.getvalue()

Peter:  Your output was helpful in seeing the difference I mentioned
above.

Thank you both for your help!

Malcolm



More information about the Python-list mailing list