blob: 20dc019feebdfa0a0b87b94fa11d78a7fc7f04fd [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{traceback} ---
Fred Drake31d10cb1999-06-29 17:08:41 +00002 Print or retrieve a stack traceback}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{traceback}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Print or retrieve a stack traceback.}
6
Guido van Rossum81b30601995-03-01 14:36:00 +00007
Guido van Rossumbca12071998-06-17 22:37:26 +00008This module provides a standard interface to extract, format and print
9stack traces of Python programs. It exactly mimics the behavior of
10the Python interpreter when it prints a stack trace. This is useful
11when you want to print stack traces under program control, e.g. in a
Guido van Rossum81b30601995-03-01 14:36:00 +000012``wrapper'' around the interpreter.
13
14The module uses traceback objects --- this is the object type
15that is stored in the variables \code{sys.exc_traceback} and
Guido van Rossumbca12071998-06-17 22:37:26 +000016\code{sys.last_traceback} and returned as the third item from
17\function{sys.exc_info()}.
Fred Drake266b4c11998-03-08 06:12:10 +000018\obindex{traceback}
Guido van Rossum81b30601995-03-01 14:36:00 +000019
20The module defines the following functions:
21
Guido van Rossumbca12071998-06-17 22:37:26 +000022\begin{funcdesc}{print_tb}{traceback\optional{, limit\optional{, file}}}
Guido van Rossum81b30601995-03-01 14:36:00 +000023Print up to \var{limit} stack trace entries from \var{traceback}. If
24\var{limit} is omitted or \code{None}, all entries are printed.
Guido van Rossumbca12071998-06-17 22:37:26 +000025If \var{file} is omitted or \code{None}, the output goes to
26\code{sys.stderr}; otherwise it should be an open file or file-like
27object to receive the output.
Guido van Rossum81b30601995-03-01 14:36:00 +000028\end{funcdesc}
29
Fred Drake43b34da1999-02-26 18:51:21 +000030\begin{funcdesc}{print_exception}{type, value, traceback\optional{,
31 limit\optional{, file}}}
Guido van Rossum81b30601995-03-01 14:36:00 +000032Print exception information and up to \var{limit} stack trace entries
Guido van Rossumbca12071998-06-17 22:37:26 +000033from \var{traceback} to \var{file}.
34This differs from \function{print_tb()} in the
Guido van Rossum81b30601995-03-01 14:36:00 +000035following ways: (1) if \var{traceback} is not \code{None}, it prints a
Fred Drake266b4c11998-03-08 06:12:10 +000036header \samp{Traceback (innermost last):}; (2) it prints the
Guido van Rossum81b30601995-03-01 14:36:00 +000037exception \var{type} and \var{value} after the stack trace; (3) if
Fred Drake266b4c11998-03-08 06:12:10 +000038\var{type} is \exception{SyntaxError} and \var{value} has the appropriate
Guido van Rossum81b30601995-03-01 14:36:00 +000039format, it prints the line where the syntax error occurred with a
Fred Drake266b4c11998-03-08 06:12:10 +000040caret indicating the approximate position of the error.
Guido van Rossum81b30601995-03-01 14:36:00 +000041\end{funcdesc}
42
Guido van Rossumbca12071998-06-17 22:37:26 +000043\begin{funcdesc}{print_exc}{\optional{limit\optional{, file}}}
Fred Drake266b4c11998-03-08 06:12:10 +000044This is a shorthand for `\code{print_exception(sys.exc_type,}
Guido van Rossumbca12071998-06-17 22:37:26 +000045\code{sys.exc_value,} \code{sys.exc_traceback,} \var{limit}\code{,}
46\var{file}\code{)}'. (In fact, it uses \code{sys.exc_info()} to
47retrieve the same information in a thread-safe way.)
Guido van Rossum81b30601995-03-01 14:36:00 +000048\end{funcdesc}
49
Guido van Rossumbca12071998-06-17 22:37:26 +000050\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
Fred Drake266b4c11998-03-08 06:12:10 +000051This is a shorthand for `\code{print_exception(sys.last_type,}
Guido van Rossumbca12071998-06-17 22:37:26 +000052\code{sys.last_value,} \code{sys.last_traceback,} \var{limit}\code{,}
53\var{file}\code{)}'.
Guido van Rossum81b30601995-03-01 14:36:00 +000054\end{funcdesc}
Guido van Rossumbca12071998-06-17 22:37:26 +000055
56\begin{funcdesc}{print_stack}{\optional{f\optional{, limit\optional{, file}}}}
57This function prints a stack trace from its invocation point. The
58optional \var{f} argument can be used to specify an alternate stack
59frame to start. The optional \var{limit} and \var{file} arguments have the
60same meaning as for \function{print_exception()}.
61\end{funcdesc}
62
Fred Drake43b34da1999-02-26 18:51:21 +000063\begin{funcdesc}{extract_tb}{traceback\optional{, limit}}
64Return a list of up to \var{limit} ``pre-processed'' stack trace
65entries extracted from the traceback object \var{traceback}. It is
66useful for alternate formatting of stack traces. If \var{limit} is
67omitted or \code{None}, all entries are extracted. A
68``pre-processed'' stack trace entry is a quadruple (\var{filename},
69\var{line number}, \var{function name}, \var{text}) representing
70the information that is usually printed for a stack trace. The
71\var{text} is a string with leading and trailing whitespace
72stripped; if the source is not available it is \code{None}.
Guido van Rossumbca12071998-06-17 22:37:26 +000073\end{funcdesc}
74
75\begin{funcdesc}{extract_stack}{\optional{f\optional{, limit}}}
76Extract the raw traceback from the current stack frame. The return
77value has the same format as for \function{extract_tb()}. The
78optional \var{f} and \var{limit} arguments have the same meaning as
79for \function{print_stack()}.
80\end{funcdesc}
81
82\begin{funcdesc}{format_list}{list}
83Given a list of tuples as returned by \function{extract_tb()} or
84\function{extract_stack()}, return a list of strings ready for
85printing. Each string in the resulting list corresponds to the item
86with the same index in the argument list. Each string ends in a
87newline; the strings may contain internal newlines as well, for those
88items whose source text line is not \code{None}.
89\end{funcdesc}
90
91\begin{funcdesc}{format_exception_only}{type, value}
92Format the exception part of a traceback. The arguments are the
93exception type and value such as given by \code{sys.last_type} and
94\code{sys.last_value}. The return value is a list of strings, each
95ending in a newline. Normally, the list contains a single string;
96however, for \code{SyntaxError} exceptions, it contains several lines
97that (when printed) display detailed information about where the
98syntax error occurred. The message indicating which exception
99occurred is the always last string in the list.
100\end{funcdesc}
101
102\begin{funcdesc}{format_exception}{type, value, tb\optional{, limit}}
103Format a stack trace and the exception information. The arguments
104have the same meaning as the corresponding arguments to
105\function{print_exception()}. The return value is a list of strings,
106each ending in a newline and some containing internal newlines. When
107these lines are contatenated and printed, exactly the same text is
108printed as does \function{print_exception()}.
109\end{funcdesc}
110
111\begin{funcdesc}{format_tb}{tb\optional{, limit}}
112A shorthand for \code{format_list(extract_tb(\var{tb}, \var{limit}))}.
113\end{funcdesc}
114
115\begin{funcdesc}{format_stack}{\optional{f\optional{, limit}}}
116A shorthand for \code{format_list(extract_stack(\var{f}, \var{limit}))}.
117\end{funcdesc}
118
119\begin{funcdesc}{tb_lineno}{tb}
120This function returns the current line number set in the traceback
121object. This is normally the same as the \code{\var{tb}.tb_lineno}
122field of the object, but when optimization is used (the -O flag) this
123field is not updated correctly; this function calculates the correct
124value.
125\end{funcdesc}
126
Fred Drake31d10cb1999-06-29 17:08:41 +0000127
128\subsection{Traceback Example \label{traceback-example}}
129
130This simple example implements a basic read-eval-print loop, similar
131to (but less useful than) the standard Python interactive interpreter
132loop. For a more complete implementation of the interpreter loop,
133refer to the \refmodule{code} module.
Guido van Rossumbca12071998-06-17 22:37:26 +0000134
135\begin{verbatim}
136import sys, traceback
137
138def run_user_code(envdir):
139 source = raw_input(">>> ")
140 try:
141 exec source in envdir
142 except:
143 print "Exception in user code:"
Guido van Rossumfaac0131998-06-17 22:38:09 +0000144 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000145 traceback.print_exc(file=sys.stdout)
Guido van Rossumfaac0131998-06-17 22:38:09 +0000146 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000147
148envdir = {}
149while 1:
150 run_user_code(envdir)
151\end{verbatim}