[Python-Dev] with_traceback

Andrew Dalke dalke at dalkescientific.com
Thu Mar 1 06:56:31 CET 2007

On 2/28/07, James Y Knight <foom at fuhm.net> wrote:
> It seems to me that a stack trace should always be attached to an
> exception object at creation time of the exception, and never at any
> other time. Then, if someone pre-creates an exception object, they
> get consistent and easily explainable behavior (the traceback to the
> creation point). The traceback won't necessarily be *useful*, but
> presumably someone pre-creating an exception object did so to save
> run-time, and creating the traceback is generally very expensive, so
> doing that only once, too, seems like a win to me.

The only example I found in about 2 dozen packages where the
exception was precreated was in pyparsing.  I don't know the reason
why it was done that way, but I'm certain it wasn't for performance.

The exception is created as part of the format definition.  In that
case if the traceback is important then it's important to know which
code was attempting the parse.  The format definition was probably
certainly done at module import time.

In any case, reraising the same exception instance is a rare
construct in current Python code.  PJE had never seen it before.
It's hard to get a good intuition from zero data points.  :)

More information about the Python-Dev mailing list