<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>3.6 traceback --
Print or retrieve a stack traceback.</TITLE>
<META NAME="description" CONTENT="3.6 traceback --
Print or retrieve a stack traceback.">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-pickle.html">
<LINK REL="previous" HREF="module-operator.html">
<LINK REL="up" HREF="python.html">
<LINK REL="next" HREF="module-pickle.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-pickle.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="python.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-operator.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-pickle.html">3.7 pickle  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="python.html">3. Python Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-operator.html">3.5 operator  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION005600000000000000000">
3.6 <tt class=module>traceback</tt> --
         Print or retrieve a stack traceback.</A>
</H1>

<P>

<P>
This module provides a standard interface to extract, format and print
stack traces of Python programs.  It exactly mimics the behavior of
the Python interpreter when it prints a stack trace.  This is useful
when you want to print stack traces under program control, e.g. in a
``wrapper'' around the interpreter.

<P>
The module uses traceback objects -- this is the object type
that is stored in the variables <tt>sys.exc_traceback</tt> and
<tt>sys.last_traceback</tt> and returned as the third item from
<tt class=function>sys.exc_info()</tt>.
<a name="l2h-393"></a>

<P>
The module defines the following functions:

<P>
<dl><dt><b><a name="l2h-380"><tt class=function>print_tb</tt></a></b> (<var>traceback</var><big>[</big><var>, limit</var><big>[</big><var>, file</var><big>]</big><big>]</big>)
<dd>
Print up to <var>limit</var> stack trace entries from <var>traceback</var>.  If
<var>limit</var> is omitted or <tt>None</tt>, all entries are printed.
If <var>file</var> is omitted or <tt>None</tt>, the output goes to
<tt>sys.stderr</tt>; otherwise it should be an open file or file-like
object to receive the output.
</dl>

<P>
<dl><dt><b><a name="l2h-381"><tt class=function>print_exception</tt></a></b> (<var>type, value, traceback</var><big>[</big><var>,
                                  limit</var><big>[</big><var>, file</var><big>]</big><big>]</big>)
<dd>
Print exception information and up to <var>limit</var> stack trace entries
from <var>traceback</var> to <var>file</var>.
This differs from <tt class=function>print_tb()</tt> in the
following ways: (1) if <var>traceback</var> is not <tt>None</tt>, it prints a
header "<tt class=samp>Traceback (innermost last):</tt>"; (2) it prints the
exception <var>type</var> and <var>value</var> after the stack trace; (3) if
<var>type</var> is <tt class=exception>SyntaxError</tt> and <var>value</var> has the appropriate
format, it prints the line where the syntax error occurred with a
caret indicating the approximate position of the error.
</dl>

<P>
<dl><dt><b><a name="l2h-382"><tt class=function>print_exc</tt></a></b> (<big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><big>]</big>)
<dd>
This is a shorthand for `<tt>print_exception(sys.exc_type,</tt><tt>sys.exc_value,</tt> <tt>sys.exc_traceback,</tt> <var>limit</var><tt>,</tt><var>file</var><tt>)</tt>'.  (In fact, it uses <tt>sys.exc_info()</tt> to
retrieve the same information in a thread-safe way.)
</dl>

<P>
<dl><dt><b><a name="l2h-383"><tt class=function>print_last</tt></a></b> (<big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><big>]</big>)
<dd>
This is a shorthand for `<tt>print_exception(sys.last_type,</tt><tt>sys.last_value,</tt> <tt>sys.last_traceback,</tt> <var>limit</var><tt>,</tt><var>file</var><tt>)</tt>'.
</dl>

<P>
<dl><dt><b><a name="l2h-384"><tt class=function>print_stack</tt></a></b> (<big>[</big><var>f</var><big>[</big><var>, limit</var><big>[</big><var>, file</var><big>]</big><big>]</big><big>]</big>)
<dd>
This function prints a stack trace from its invocation point.  The
optional <var>f</var> argument can be used to specify an alternate stack
frame to start.  The optional <var>limit</var> and <var>file</var> arguments have the
same meaning as for <tt class=function>print_exception()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-385"><tt class=function>extract_tb</tt></a></b> (<var>traceback</var><big>[</big><var>, limit</var><big>]</big>)
<dd>
Return a list of up to <var>limit</var> ``pre-processed'' stack trace
entries extracted from the traceback object <var>traceback</var>.  It is
useful for alternate formatting of stack traces.  If <var>limit</var> is
omitted or <tt>None</tt>, all entries are extracted.  A
``pre-processed'' stack trace entry is a quadruple (<var>filename</var>,
<var>line number</var>, <var>function name</var>, <var>text</var>) representing
the information that is usually printed for a stack trace.  The
<var>text</var> is a string with leading and trailing whitespace
stripped; if the source is not available it is <tt>None</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-386"><tt class=function>extract_stack</tt></a></b> (<big>[</big><var>f</var><big>[</big><var>, limit</var><big>]</big><big>]</big>)
<dd>
Extract the raw traceback from the current stack frame.  The return
value has the same format as for <tt class=function>extract_tb()</tt>.  The
optional <var>f</var> and <var>limit</var> arguments have the same meaning as
for <tt class=function>print_stack()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-387"><tt class=function>format_list</tt></a></b> (<var>list</var>)
<dd>
Given a list of tuples as returned by <tt class=function>extract_tb()</tt> or
<tt class=function>extract_stack()</tt>, return a list of strings ready for
printing.  Each string in the resulting list corresponds to the item
with the same index in the argument list.  Each string ends in a
newline; the strings may contain internal newlines as well, for those
items whose source text line is not <tt>None</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-388"><tt class=function>format_exception_only</tt></a></b> (<var>type, value</var>)
<dd>
Format the exception part of a traceback.  The arguments are the
exception type and value such as given by <tt>sys.last_type</tt> and
<tt>sys.last_value</tt>.  The return value is a list of strings, each
ending in a newline.  Normally, the list contains a single string;
however, for <tt>SyntaxError</tt> exceptions, it contains several lines
that (when printed) display detailed information about where the
syntax error occurred.  The message indicating which exception
occurred is the always last string in the list.
</dl>

<P>
<dl><dt><b><a name="l2h-389"><tt class=function>format_exception</tt></a></b> (<var>type, value, tb</var><big>[</big><var>, limit</var><big>]</big>)
<dd>
Format a stack trace and the exception information.  The arguments 
have the same meaning as the corresponding arguments to
<tt class=function>print_exception()</tt>.  The return value is a list of strings,
each ending in a newline and some containing internal newlines.  When
these lines are contatenated and printed, exactly the same text is
printed as does <tt class=function>print_exception()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-390"><tt class=function>format_tb</tt></a></b> (<var>tb</var><big>[</big><var>, limit</var><big>]</big>)
<dd>
A shorthand for <tt>format_list(extract_tb(<var>tb</var>, <var>limit</var>))</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-391"><tt class=function>format_stack</tt></a></b> (<big>[</big><var>f</var><big>[</big><var>, limit</var><big>]</big><big>]</big>)
<dd>
A shorthand for <tt>format_list(extract_stack(<var>f</var>, <var>limit</var>))</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-392"><tt class=function>tb_lineno</tt></a></b> (<var>tb</var>)
<dd>
This function returns the current line number set in the traceback
object.  This is normally the same as the <tt><var>tb</var>.tb_lineno</tt>field of the object, but when optimization is used (the -O flag) this
field is not updated correctly; this function calculates the correct
value.
</dl>

<P>
A simple example follows:

<P>
<dl><dd><pre class=verbatim>
import sys, traceback

def run_user_code(envdir):
    source = raw_input("&gt;&gt;&gt; ")
    try:
        exec source in envdir
    except:
        print "Exception in user code:"
        print '-'*60
        traceback.print_exc(file=sys.stdout)
        print '-'*60

envdir = {}
while 1:
    run_user_code(envdir)
</pre></dl><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-pickle.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="python.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-operator.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-pickle.html">3.7 pickle  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="python.html">3. Python Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-operator.html">3.5 operator  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
