blob: dd4feb18d2d792e6b8786cc144f07243a8bc6f05 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{traceback}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-traceback}
Guido van Rossum81b30601995-03-01 14:36:00 +00003\stmodindex{traceback}
4
Guido van Rossum81b30601995-03-01 14:36:00 +00005
6This module provides a standard interface to format and print stack
7traces of Python programs. It exactly mimics the behavior of the
8Python interpreter when it prints a stack trace. This is useful when
9you want to print stack traces under program control, e.g. in a
10``wrapper'' around the interpreter.
11
12The module uses traceback objects --- this is the object type
13that is stored in the variables \code{sys.exc_traceback} and
14\code{sys.last_traceback}.
Fred Drake266b4c11998-03-08 06:12:10 +000015\obindex{traceback}
Guido van Rossum81b30601995-03-01 14:36:00 +000016
17The module defines the following functions:
18
Fred Drake266b4c11998-03-08 06:12:10 +000019\begin{funcdesc}{print_tb}{traceback\optional{, limit}}
Guido van Rossum81b30601995-03-01 14:36:00 +000020Print up to \var{limit} stack trace entries from \var{traceback}. If
21\var{limit} is omitted or \code{None}, all entries are printed.
22\end{funcdesc}
23
Fred Drake266b4c11998-03-08 06:12:10 +000024\begin{funcdesc}{extract_tb}{traceback\optional{, limit}}
Guido van Rossum81b30601995-03-01 14:36:00 +000025Return a list of up to \var{limit} ``pre-processed'' stack trace
26entries extracted from \var{traceback}. It is useful for alternate
27formatting of stack traces. If \var{limit} is omitted or \code{None},
28all entries are extracted. A ``pre-processed'' stack trace entry is a
29quadruple (\var{filename}, \var{line number}, \var{function name},
30\var{line text}) representing the information that is usually printed
31for a stack trace. The \var{line text} is a string with leading and
32trailing whitespace stripped; if the source is not available it is
33\code{None}.
34\end{funcdesc}
35
Fred Drake266b4c11998-03-08 06:12:10 +000036\begin{funcdesc}{print_exception}{type, value, traceback\optional{, limit}}
Guido van Rossum81b30601995-03-01 14:36:00 +000037Print exception information and up to \var{limit} stack trace entries
Fred Drake266b4c11998-03-08 06:12:10 +000038from \var{traceback}. This differs from \function{print_tb()} in the
Guido van Rossum81b30601995-03-01 14:36:00 +000039following ways: (1) if \var{traceback} is not \code{None}, it prints a
Fred Drake266b4c11998-03-08 06:12:10 +000040header \samp{Traceback (innermost last):}; (2) it prints the
Guido van Rossum81b30601995-03-01 14:36:00 +000041exception \var{type} and \var{value} after the stack trace; (3) if
Fred Drake266b4c11998-03-08 06:12:10 +000042\var{type} is \exception{SyntaxError} and \var{value} has the appropriate
Guido van Rossum81b30601995-03-01 14:36:00 +000043format, it prints the line where the syntax error occurred with a
Fred Drake266b4c11998-03-08 06:12:10 +000044caret indicating the approximate position of the error.
Guido van Rossum81b30601995-03-01 14:36:00 +000045\end{funcdesc}
46
47\begin{funcdesc}{print_exc}{\optional{limit}}
Fred Drake266b4c11998-03-08 06:12:10 +000048This is a shorthand for `\code{print_exception(sys.exc_type,}
49\code{sys.exc_value,} \code{sys.exc_traceback,} \var{limit}\code{)}'.
Guido van Rossum81b30601995-03-01 14:36:00 +000050\end{funcdesc}
51
52\begin{funcdesc}{print_last}{\optional{limit}}
Fred Drake266b4c11998-03-08 06:12:10 +000053This is a shorthand for `\code{print_exception(sys.last_type,}
54\code{sys.last_value,} \code{sys.last_traceback,} \var{limit}\code{)}'.
Guido van Rossum81b30601995-03-01 14:36:00 +000055\end{funcdesc}