blob: 735b8de5f282ad1635fec7416190fa6827e59ba2 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{traceback} ---
2 Print or retrieve a stack traceback.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{traceback}
4
5\modulesynopsis{Print or retrieve a stack traceback.}
6
Guido van Rossum81b30601995-03-01 14:36:00 +00007
Guido van Rossum81b30601995-03-01 14:36:00 +00008
Guido van Rossumbca12071998-06-17 22:37:26 +00009This module provides a standard interface to extract, format and print
10stack traces of Python programs. It exactly mimics the behavior of
11the Python interpreter when it prints a stack trace. This is useful
12when you want to print stack traces under program control, e.g. in a
Guido van Rossum81b30601995-03-01 14:36:00 +000013``wrapper'' around the interpreter.
14
15The module uses traceback objects --- this is the object type
16that is stored in the variables \code{sys.exc_traceback} and
Guido van Rossumbca12071998-06-17 22:37:26 +000017\code{sys.last_traceback} and returned as the third item from
18\function{sys.exc_info()}.
Fred Drake266b4c11998-03-08 06:12:10 +000019\obindex{traceback}
Guido van Rossum81b30601995-03-01 14:36:00 +000020
21The module defines the following functions:
22
Guido van Rossumbca12071998-06-17 22:37:26 +000023\begin{funcdesc}{print_tb}{traceback\optional{, limit\optional{, file}}}
Guido van Rossum81b30601995-03-01 14:36:00 +000024Print up to \var{limit} stack trace entries from \var{traceback}. If
25\var{limit} is omitted or \code{None}, all entries are printed.
Guido van Rossumbca12071998-06-17 22:37:26 +000026If \var{file} is omitted or \code{None}, the output goes to
27\code{sys.stderr}; otherwise it should be an open file or file-like
28object to receive the output.
Guido van Rossum81b30601995-03-01 14:36:00 +000029\end{funcdesc}
30
Fred Drake43b34da1999-02-26 18:51:21 +000031\begin{funcdesc}{print_exception}{type, value, traceback\optional{,
32 limit\optional{, file}}}
Guido van Rossum81b30601995-03-01 14:36:00 +000033Print exception information and up to \var{limit} stack trace entries
Guido van Rossumbca12071998-06-17 22:37:26 +000034from \var{traceback} to \var{file}.
35This differs from \function{print_tb()} in the
Guido van Rossum81b30601995-03-01 14:36:00 +000036following ways: (1) if \var{traceback} is not \code{None}, it prints a
Fred Drake266b4c11998-03-08 06:12:10 +000037header \samp{Traceback (innermost last):}; (2) it prints the
Guido van Rossum81b30601995-03-01 14:36:00 +000038exception \var{type} and \var{value} after the stack trace; (3) if
Fred Drake266b4c11998-03-08 06:12:10 +000039\var{type} is \exception{SyntaxError} and \var{value} has the appropriate
Guido van Rossum81b30601995-03-01 14:36:00 +000040format, it prints the line where the syntax error occurred with a
Fred Drake266b4c11998-03-08 06:12:10 +000041caret indicating the approximate position of the error.
Guido van Rossum81b30601995-03-01 14:36:00 +000042\end{funcdesc}
43
Guido van Rossumbca12071998-06-17 22:37:26 +000044\begin{funcdesc}{print_exc}{\optional{limit\optional{, file}}}
Fred Drake266b4c11998-03-08 06:12:10 +000045This is a shorthand for `\code{print_exception(sys.exc_type,}
Guido van Rossumbca12071998-06-17 22:37:26 +000046\code{sys.exc_value,} \code{sys.exc_traceback,} \var{limit}\code{,}
47\var{file}\code{)}'. (In fact, it uses \code{sys.exc_info()} to
48retrieve the same information in a thread-safe way.)
Guido van Rossum81b30601995-03-01 14:36:00 +000049\end{funcdesc}
50
Guido van Rossumbca12071998-06-17 22:37:26 +000051\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
Fred Drake266b4c11998-03-08 06:12:10 +000052This is a shorthand for `\code{print_exception(sys.last_type,}
Guido van Rossumbca12071998-06-17 22:37:26 +000053\code{sys.last_value,} \code{sys.last_traceback,} \var{limit}\code{,}
54\var{file}\code{)}'.
Guido van Rossum81b30601995-03-01 14:36:00 +000055\end{funcdesc}
Guido van Rossumbca12071998-06-17 22:37:26 +000056
57\begin{funcdesc}{print_stack}{\optional{f\optional{, limit\optional{, file}}}}
58This function prints a stack trace from its invocation point. The
59optional \var{f} argument can be used to specify an alternate stack
60frame to start. The optional \var{limit} and \var{file} arguments have the
61same meaning as for \function{print_exception()}.
62\end{funcdesc}
63
Fred Drake43b34da1999-02-26 18:51:21 +000064\begin{funcdesc}{extract_tb}{traceback\optional{, limit}}
65Return a list of up to \var{limit} ``pre-processed'' stack trace
66entries extracted from the traceback object \var{traceback}. It is
67useful for alternate formatting of stack traces. If \var{limit} is
68omitted or \code{None}, all entries are extracted. A
69``pre-processed'' stack trace entry is a quadruple (\var{filename},
70\var{line number}, \var{function name}, \var{text}) representing
71the information that is usually printed for a stack trace. The
72\var{text} is a string with leading and trailing whitespace
73stripped; if the source is not available it is \code{None}.
Guido van Rossumbca12071998-06-17 22:37:26 +000074\end{funcdesc}
75
76\begin{funcdesc}{extract_stack}{\optional{f\optional{, limit}}}
77Extract the raw traceback from the current stack frame. The return
78value has the same format as for \function{extract_tb()}. The
79optional \var{f} and \var{limit} arguments have the same meaning as
80for \function{print_stack()}.
81\end{funcdesc}
82
83\begin{funcdesc}{format_list}{list}
84Given a list of tuples as returned by \function{extract_tb()} or
85\function{extract_stack()}, return a list of strings ready for
86printing. Each string in the resulting list corresponds to the item
87with the same index in the argument list. Each string ends in a
88newline; the strings may contain internal newlines as well, for those
89items whose source text line is not \code{None}.
90\end{funcdesc}
91
92\begin{funcdesc}{format_exception_only}{type, value}
93Format the exception part of a traceback. The arguments are the
94exception type and value such as given by \code{sys.last_type} and
95\code{sys.last_value}. The return value is a list of strings, each
96ending in a newline. Normally, the list contains a single string;
97however, for \code{SyntaxError} exceptions, it contains several lines
98that (when printed) display detailed information about where the
99syntax error occurred. The message indicating which exception
100occurred is the always last string in the list.
101\end{funcdesc}
102
103\begin{funcdesc}{format_exception}{type, value, tb\optional{, limit}}
104Format a stack trace and the exception information. The arguments
105have the same meaning as the corresponding arguments to
106\function{print_exception()}. The return value is a list of strings,
107each ending in a newline and some containing internal newlines. When
108these lines are contatenated and printed, exactly the same text is
109printed as does \function{print_exception()}.
110\end{funcdesc}
111
112\begin{funcdesc}{format_tb}{tb\optional{, limit}}
113A shorthand for \code{format_list(extract_tb(\var{tb}, \var{limit}))}.
114\end{funcdesc}
115
116\begin{funcdesc}{format_stack}{\optional{f\optional{, limit}}}
117A shorthand for \code{format_list(extract_stack(\var{f}, \var{limit}))}.
118\end{funcdesc}
119
120\begin{funcdesc}{tb_lineno}{tb}
121This function returns the current line number set in the traceback
122object. This is normally the same as the \code{\var{tb}.tb_lineno}
123field of the object, but when optimization is used (the -O flag) this
124field is not updated correctly; this function calculates the correct
125value.
126\end{funcdesc}
127
128A simple example follows:
129
130\begin{verbatim}
131import sys, traceback
132
133def run_user_code(envdir):
134 source = raw_input(">>> ")
135 try:
136 exec source in envdir
137 except:
138 print "Exception in user code:"
Guido van Rossumfaac0131998-06-17 22:38:09 +0000139 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000140 traceback.print_exc(file=sys.stdout)
Guido van Rossumfaac0131998-06-17 22:38:09 +0000141 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000142
143envdir = {}
144while 1:
145 run_user_code(envdir)
146\end{verbatim}