Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111886 > unrolled thread

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

Started byMalcolm Greene <python@bdurham.com>
First post2016-07-26 06:11 -0400
Last post2016-07-26 23:40 +1000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Possible to capture cgitb style output in a try/except section? Malcolm Greene <python@bdurham.com> - 2016-07-26 06:11 -0400
    Re: Possible to capture cgitb style output in a try/except section? Steven D'Aprano <steve@pearwood.info> - 2016-07-26 23:40 +1000

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

FromMalcolm Greene <python@bdurham.com>
Date2016-07-26 06:11 -0400
SubjectPossible to capture cgitb style output in a try/except section?
Message-ID<mailman.125.1469527867.22221.python-list@python.org>
Is there a way to capture cgitb's extensive output in an except clause
so that cgitb's detailed traceback output can be logged *and* the except
section can handle the exception so the script can continue running?
 
My read of the cgitb documentation leads me to believe that the only way
I can get cgitb output is to let an exception propagate to the point of
terminating my script ... at which point cgitb grabs the exception and
does its magic.
 
Thank you,
Malcolm

[toc] | [next] | [standalone]


#111889

FromSteven D'Aprano <steve@pearwood.info>
Date2016-07-26 23:40 +1000
Message-ID<5797684c$0$1591$c3e8da3$5496439d@news.astraweb.com>
In reply to#111886
On Tue, 26 Jul 2016 08:11 pm, Malcolm Greene wrote:

> Is there a way to capture cgitb's extensive output in an except clause
> so that cgitb's detailed traceback output can be logged *and* the except
> section can handle the exception so the script can continue running?

Anything that cgitb captures in a traceback can be captured at any time and
processed anyway you like. There's no real magic happening in cgitb, you
can read the source and program your own traceback handler that records any
details you like.

Or you can use the cgitb module as-is, and capture the output:


py> from StringIO import StringIO
py> s = StringIO()
py> import cgitb
py> handler = cgitb.Hook(file=s, format="text").handle
py> try:
...     x = 1/0
... except Exception as e:
...     handler()
...
py> text = s.getvalue()
py> print text
<type 'exceptions.ZeroDivisionError'>
Python 2.7.2: /usr/local/bin/python2.7
Tue Jul 26 23:37:06 2016

A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.

[...]

The above is a description of an error in a Python program.  Here is
the original traceback:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web