blob: eee898e14fc47bda191d5d4adcb98b4ebb24e89a [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
Fred Drakedb40afa2002-07-25 21:11:23 +000011when you want to print stack traces under program control, such as in a
Guido van Rossum81b30601995-03-01 14:36:00 +000012``wrapper'' around the interpreter.
13
Fred Drakedb40afa2002-07-25 21:11:23 +000014The module uses traceback objects --- this is the object type that is
15stored in the variables \code{sys.exc_traceback} (deprecated) 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 Drake162c6a62001-02-14 03:20:18 +000036header \samp{Traceback (most recent call 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 Drakedb40afa2002-07-25 21:11:23 +000038\var{type} is \exception{SyntaxError} and \var{value} has the
39appropriate format, it prints the line where the syntax error occurred
40with a caret 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 Drakedb40afa2002-07-25 21:11:23 +000044This is a shorthand for \code{print_exception(sys.exc_type,
45sys.exc_value, sys.exc_traceback, \var{limit}, \var{file})}. (In
46fact, it uses \function{sys.exc_info()} to retrieve the same
47information in a thread-safe way instead of using the deprecated
48variables.)
Guido van Rossum81b30601995-03-01 14:36:00 +000049\end{funcdesc}
50
Neil Schemenauerf607fc52003-11-05 23:03:00 +000051\begin{funcdesc}{format_exc}{\optional{limit\optional{, file}}}
52This is like \code{print_exc(\var{limit})} but returns a string
53instead of printing to a file.
54\end{funcdesc}
55
Guido van Rossumbca12071998-06-17 22:37:26 +000056\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
Fred Drakedb40afa2002-07-25 21:11:23 +000057This is a shorthand for \code{print_exception(sys.last_type,
58sys.last_value, sys.last_traceback, \var{limit}, \var{file})}.
Guido van Rossum81b30601995-03-01 14:36:00 +000059\end{funcdesc}
Guido van Rossumbca12071998-06-17 22:37:26 +000060
61\begin{funcdesc}{print_stack}{\optional{f\optional{, limit\optional{, file}}}}
62This function prints a stack trace from its invocation point. The
63optional \var{f} argument can be used to specify an alternate stack
64frame to start. The optional \var{limit} and \var{file} arguments have the
65same meaning as for \function{print_exception()}.
66\end{funcdesc}
67
Fred Drake43b34da1999-02-26 18:51:21 +000068\begin{funcdesc}{extract_tb}{traceback\optional{, limit}}
69Return a list of up to \var{limit} ``pre-processed'' stack trace
70entries extracted from the traceback object \var{traceback}. It is
71useful for alternate formatting of stack traces. If \var{limit} is
72omitted or \code{None}, all entries are extracted. A
73``pre-processed'' stack trace entry is a quadruple (\var{filename},
74\var{line number}, \var{function name}, \var{text}) representing
75the information that is usually printed for a stack trace. The
76\var{text} is a string with leading and trailing whitespace
77stripped; if the source is not available it is \code{None}.
Guido van Rossumbca12071998-06-17 22:37:26 +000078\end{funcdesc}
79
80\begin{funcdesc}{extract_stack}{\optional{f\optional{, limit}}}
81Extract the raw traceback from the current stack frame. The return
82value has the same format as for \function{extract_tb()}. The
83optional \var{f} and \var{limit} arguments have the same meaning as
84for \function{print_stack()}.
85\end{funcdesc}
86
87\begin{funcdesc}{format_list}{list}
88Given a list of tuples as returned by \function{extract_tb()} or
89\function{extract_stack()}, return a list of strings ready for
90printing. Each string in the resulting list corresponds to the item
91with the same index in the argument list. Each string ends in a
92newline; the strings may contain internal newlines as well, for those
93items whose source text line is not \code{None}.
94\end{funcdesc}
95
96\begin{funcdesc}{format_exception_only}{type, value}
97Format the exception part of a traceback. The arguments are the
98exception type and value such as given by \code{sys.last_type} and
99\code{sys.last_value}. The return value is a list of strings, each
100ending in a newline. Normally, the list contains a single string;
Fred Drakedb40afa2002-07-25 21:11:23 +0000101however, for \exception{SyntaxError} exceptions, it contains several
102lines that (when printed) display detailed information about where the
Guido van Rossumbca12071998-06-17 22:37:26 +0000103syntax error occurred. The message indicating which exception
104occurred is the always last string in the list.
105\end{funcdesc}
106
107\begin{funcdesc}{format_exception}{type, value, tb\optional{, limit}}
108Format a stack trace and the exception information. The arguments
109have the same meaning as the corresponding arguments to
110\function{print_exception()}. The return value is a list of strings,
111each ending in a newline and some containing internal newlines. When
Thomas Woutersf8316632000-07-16 19:01:10 +0000112these lines are concatenated and printed, exactly the same text is
Guido van Rossumbca12071998-06-17 22:37:26 +0000113printed as does \function{print_exception()}.
114\end{funcdesc}
115
116\begin{funcdesc}{format_tb}{tb\optional{, limit}}
117A shorthand for \code{format_list(extract_tb(\var{tb}, \var{limit}))}.
118\end{funcdesc}
119
120\begin{funcdesc}{format_stack}{\optional{f\optional{, limit}}}
121A shorthand for \code{format_list(extract_stack(\var{f}, \var{limit}))}.
122\end{funcdesc}
123
124\begin{funcdesc}{tb_lineno}{tb}
125This function returns the current line number set in the traceback
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000126object. This function was necessary because in versions of Python
Fred Drake008a36a2003-01-30 22:22:59 +0000127prior to 2.3 when the \programopt{-O} flag was passed to Python the
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000128\code{\var{tb}.tb_lineno} was not updated correctly. This function
129has no use in versions past 2.3.
Guido van Rossumbca12071998-06-17 22:37:26 +0000130\end{funcdesc}
131
Fred Drake31d10cb1999-06-29 17:08:41 +0000132
133\subsection{Traceback Example \label{traceback-example}}
134
135This simple example implements a basic read-eval-print loop, similar
136to (but less useful than) the standard Python interactive interpreter
137loop. For a more complete implementation of the interpreter loop,
138refer to the \refmodule{code} module.
Guido van Rossumbca12071998-06-17 22:37:26 +0000139
140\begin{verbatim}
141import sys, traceback
142
143def run_user_code(envdir):
144 source = raw_input(">>> ")
145 try:
146 exec source in envdir
147 except:
148 print "Exception in user code:"
Guido van Rossumfaac0131998-06-17 22:38:09 +0000149 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000150 traceback.print_exc(file=sys.stdout)
Guido van Rossumfaac0131998-06-17 22:38:09 +0000151 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000152
153envdir = {}
154while 1:
155 run_user_code(envdir)
156\end{verbatim}