Quick search

I’ve always found it a bit hard to visually parse tracebacks in Python. The
ColorTB class is a solution to that problem. It colors the different parts of a
traceback in a manner similar to what you would expect from a syntax-highlighting
text editor.

Installation instructions for ColorTB:

import sys,ultratb
sys.excepthook = ultratb.ColorTB()

VerboseTB

I’ve also included a port of Ka-Ping Yee’s “cgitb.py” that produces all kinds
of useful info when a traceback occurs. Ping originally had it spit out HTML
and intended it for CGI programmers, but why should they have all the fun? I
altered it to spit out colored text to the terminal. It’s a bit overwhelming,
but kind of neat, and maybe useful for long-running programs that you believe
are bug-free. If a crash does occur in that type of program you want details.
Give it a shot–you’ll love it or you’ll hate it.

Note:

The Verbose mode prints the variables currently visible where the exception
happened (shortening their strings if too long). This can potentially be
very slow, if you happen to have a huge data structure whose string
representation is complex to compute. Your computer may appear to freeze for
a while with cpu usage at 100%. If this occurs, you can cancel the traceback
with Ctrl-C (maybe hitting it more than once).

If you encounter this kind of situation often, you may want to use the
Verbose_novars mode instead of the regular Verbose, which avoids formatting
variables (but otherwise includes the information and context given by
Verbose).

Installation instructions for ColorTB:

import sys,ultratb
sys.excepthook = ultratb.VerboseTB()

Note: Much of the code in this module was lifted verbatim from the standard
library module ‘traceback.py’ and Ka-Ping Yee’s ‘cgitb.py’.

Color schemes

The colors are defined in the class TBTools through the use of the
ColorSchemeTable class. Currently the following exist:

NoColor: allows all of this module to be used in any terminal (the color

escapes are just dummy blank strings).

Linux: is meant to look good in a terminal like the Linux console (black

or very dark background).

LightBG: similar to Linux but swaps dark/light colors to be more readable

in light background terminals.

You can implement other color schemes easily, the syntax is fairly
self-explanatory. Please send back new schemes you develop to the author for
possible inclusion in future releases.

Call up the pdb debugger if desired, always clean up the tb
reference.

Keywords:

force(False): by default, this routine checks the instance call_pdb

flag and does not actually invoke the debugger if the flag is false.
The ‘force’ option forces the debugger to activate even if the flag
is false.

If the call_pdb flag is set, the pdb interactive debugger is
invoked. In all cases, the self.tb reference to the current traceback
is deleted to prevent lingering references which hamper memory
management.

Note that each call to pdb() does an ‘import readline’, so if your app
requires a special setup for the readline completers, you’ll have to
fix that by hand after invoking the exception handler.

Call up the pdb debugger if desired, always clean up the tb
reference.

Keywords:

force(False): by default, this routine checks the instance call_pdb

flag and does not actually invoke the debugger if the flag is false.
The ‘force’ option forces the debugger to activate even if the flag
is false.

If the call_pdb flag is set, the pdb interactive debugger is
invoked. In all cases, the self.tb reference to the current traceback
is deleted to prevent lingering references which hamper memory
management.

Note that each call to pdb() does an ‘import readline’, so if your app
requires a special setup for the readline completers, you’ll have to
fix that by hand after invoking the exception handler.

Allows a tb_offset to be specified. This is useful for situations where
one needs to remove a number of topmost frames from the traceback (such as
occurs with python programs that themselves execute other python code,
like Python shells).

Call up the pdb debugger if desired, always clean up the tb
reference.

Keywords:

force(False): by default, this routine checks the instance call_pdb

flag and does not actually invoke the debugger if the flag is false.
The ‘force’ option forces the debugger to activate even if the flag
is false.

If the call_pdb flag is set, the pdb interactive debugger is
invoked. In all cases, the self.tb reference to the current traceback
is deleted to prevent lingering references which hamper memory
management.

Note that each call to pdb() does an ‘import readline’, so if your app
requires a special setup for the readline completers, you’ll have to
fix that by hand after invoking the exception handler.

Define how many frames to drop from the tracebacks. Calling it with
tb_offset=1 allows use of this handler in interpreters which will have
their own code at the top of the traceback (VerboseTB will first
remove that frame before printing the traceback info).

Call up the pdb debugger if desired, always clean up the tb
reference.

Keywords:

force(False): by default, this routine checks the instance call_pdb

flag and does not actually invoke the debugger if the flag is false.
The ‘force’ option forces the debugger to activate even if the flag
is false.

If the call_pdb flag is set, the pdb interactive debugger is
invoked. In all cases, the self.tb reference to the current traceback
is deleted to prevent lingering references which hamper memory
management.

Note that each call to pdb() does an ‘import readline’, so if your app
requires a special setup for the readline completers, you’ll have to
fix that by hand after invoking the exception handler.

The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list. An IOError
is raised if the source code cannot be retrieved.

FIXED version with which we monkeypatch the stdlib to work around a bug.