blob: 1ad165c0181aa56d04649c8deeb1cbfbfcdf6b43 [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.
Neal Norwitz378f7b52003-12-13 22:34:09 +000054\versionadded{2.4}
Neil Schemenauerf607fc52003-11-05 23:03:00 +000055\end{funcdesc}
56
Guido van Rossumbca12071998-06-17 22:37:26 +000057\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
Fred Drakedb40afa2002-07-25 21:11:23 +000058This is a shorthand for \code{print_exception(sys.last_type,
59sys.last_value, sys.last_traceback, \var{limit}, \var{file})}.
Guido van Rossum81b30601995-03-01 14:36:00 +000060\end{funcdesc}
Guido van Rossumbca12071998-06-17 22:37:26 +000061
62\begin{funcdesc}{print_stack}{\optional{f\optional{, limit\optional{, file}}}}
63This function prints a stack trace from its invocation point. The
64optional \var{f} argument can be used to specify an alternate stack
65frame to start. The optional \var{limit} and \var{file} arguments have the
66same meaning as for \function{print_exception()}.
67\end{funcdesc}
68
Fred Drake43b34da1999-02-26 18:51:21 +000069\begin{funcdesc}{extract_tb}{traceback\optional{, limit}}
70Return a list of up to \var{limit} ``pre-processed'' stack trace
71entries extracted from the traceback object \var{traceback}. It is
72useful for alternate formatting of stack traces. If \var{limit} is
73omitted or \code{None}, all entries are extracted. A
74``pre-processed'' stack trace entry is a quadruple (\var{filename},
75\var{line number}, \var{function name}, \var{text}) representing
76the information that is usually printed for a stack trace. The
77\var{text} is a string with leading and trailing whitespace
78stripped; if the source is not available it is \code{None}.
Guido van Rossumbca12071998-06-17 22:37:26 +000079\end{funcdesc}
80
81\begin{funcdesc}{extract_stack}{\optional{f\optional{, limit}}}
82Extract the raw traceback from the current stack frame. The return
83value has the same format as for \function{extract_tb()}. The
84optional \var{f} and \var{limit} arguments have the same meaning as
85for \function{print_stack()}.
86\end{funcdesc}
87
88\begin{funcdesc}{format_list}{list}
89Given a list of tuples as returned by \function{extract_tb()} or
90\function{extract_stack()}, return a list of strings ready for
91printing. Each string in the resulting list corresponds to the item
92with the same index in the argument list. Each string ends in a
93newline; the strings may contain internal newlines as well, for those
94items whose source text line is not \code{None}.
95\end{funcdesc}
96
97\begin{funcdesc}{format_exception_only}{type, value}
98Format the exception part of a traceback. The arguments are the
99exception type and value such as given by \code{sys.last_type} and
100\code{sys.last_value}. The return value is a list of strings, each
101ending in a newline. Normally, the list contains a single string;
Fred Drakedb40afa2002-07-25 21:11:23 +0000102however, for \exception{SyntaxError} exceptions, it contains several
103lines that (when printed) display detailed information about where the
Guido van Rossumbca12071998-06-17 22:37:26 +0000104syntax error occurred. The message indicating which exception
105occurred is the always last string in the list.
106\end{funcdesc}
107
108\begin{funcdesc}{format_exception}{type, value, tb\optional{, limit}}
109Format a stack trace and the exception information. The arguments
110have the same meaning as the corresponding arguments to
111\function{print_exception()}. The return value is a list of strings,
112each ending in a newline and some containing internal newlines. When
Thomas Woutersf8316632000-07-16 19:01:10 +0000113these lines are concatenated and printed, exactly the same text is
Guido van Rossumbca12071998-06-17 22:37:26 +0000114printed as does \function{print_exception()}.
115\end{funcdesc}
116
117\begin{funcdesc}{format_tb}{tb\optional{, limit}}
118A shorthand for \code{format_list(extract_tb(\var{tb}, \var{limit}))}.
119\end{funcdesc}
120
121\begin{funcdesc}{format_stack}{\optional{f\optional{, limit}}}
122A shorthand for \code{format_list(extract_stack(\var{f}, \var{limit}))}.
123\end{funcdesc}
124
125\begin{funcdesc}{tb_lineno}{tb}
126This function returns the current line number set in the traceback
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000127object. This function was necessary because in versions of Python
Fred Drake008a36a2003-01-30 22:22:59 +0000128prior to 2.3 when the \programopt{-O} flag was passed to Python the
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000129\code{\var{tb}.tb_lineno} was not updated correctly. This function
130has no use in versions past 2.3.
Guido van Rossumbca12071998-06-17 22:37:26 +0000131\end{funcdesc}
132
Fred Drake31d10cb1999-06-29 17:08:41 +0000133
134\subsection{Traceback Example \label{traceback-example}}
135
136This simple example implements a basic read-eval-print loop, similar
137to (but less useful than) the standard Python interactive interpreter
138loop. For a more complete implementation of the interpreter loop,
139refer to the \refmodule{code} module.
Guido van Rossumbca12071998-06-17 22:37:26 +0000140
141\begin{verbatim}
142import sys, traceback
143
144def run_user_code(envdir):
145 source = raw_input(">>> ")
146 try:
147 exec source in envdir
148 except:
149 print "Exception in user code:"
Guido van Rossumfaac0131998-06-17 22:38:09 +0000150 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000151 traceback.print_exc(file=sys.stdout)
Guido van Rossumfaac0131998-06-17 22:38:09 +0000152 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000153
154envdir = {}
155while 1:
156 run_user_code(envdir)
157\end{verbatim}