blob: a87b1ef34d611fba4f1878566b90046c59ad3d46 [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
Neal Norwitzac3625f2006-03-17 05:49:33 +000015stored in the \code{sys.last_traceback} variable and returned
16as the third item from \function{sys.exc_info()}.
Fred Drake266b4c11998-03-08 06:12:10 +000017\obindex{traceback}
Guido van Rossum81b30601995-03-01 14:36:00 +000018
19The module defines the following functions:
20
Guido van Rossumbca12071998-06-17 22:37:26 +000021\begin{funcdesc}{print_tb}{traceback\optional{, limit\optional{, file}}}
Guido van Rossum81b30601995-03-01 14:36:00 +000022Print up to \var{limit} stack trace entries from \var{traceback}. If
23\var{limit} is omitted or \code{None}, all entries are printed.
Guido van Rossumbca12071998-06-17 22:37:26 +000024If \var{file} is omitted or \code{None}, the output goes to
25\code{sys.stderr}; otherwise it should be an open file or file-like
26object to receive the output.
Guido van Rossum81b30601995-03-01 14:36:00 +000027\end{funcdesc}
28
Fred Drake43b34da1999-02-26 18:51:21 +000029\begin{funcdesc}{print_exception}{type, value, traceback\optional{,
30 limit\optional{, file}}}
Guido van Rossum81b30601995-03-01 14:36:00 +000031Print exception information and up to \var{limit} stack trace entries
Guido van Rossumbca12071998-06-17 22:37:26 +000032from \var{traceback} to \var{file}.
33This differs from \function{print_tb()} in the
Guido van Rossum81b30601995-03-01 14:36:00 +000034following ways: (1) if \var{traceback} is not \code{None}, it prints a
Fred Drake162c6a62001-02-14 03:20:18 +000035header \samp{Traceback (most recent call last):}; (2) it prints the
Guido van Rossum81b30601995-03-01 14:36:00 +000036exception \var{type} and \var{value} after the stack trace; (3) if
Fred Drakedb40afa2002-07-25 21:11:23 +000037\var{type} is \exception{SyntaxError} and \var{value} has the
38appropriate format, it prints the line where the syntax error occurred
39with a caret indicating the approximate position of the error.
Guido van Rossum81b30601995-03-01 14:36:00 +000040\end{funcdesc}
41
Guido van Rossumbca12071998-06-17 22:37:26 +000042\begin{funcdesc}{print_exc}{\optional{limit\optional{, file}}}
Neal Norwitzac3625f2006-03-17 05:49:33 +000043This is a shorthand for \code{print_exception(*\function{sys.exc_info()}}.
Guido van Rossum81b30601995-03-01 14:36:00 +000044\end{funcdesc}
45
Georg Brandl071ae4c2005-12-16 19:21:05 +000046\begin{funcdesc}{format_exc}{\optional{limit}}
Neil Schemenauerf607fc52003-11-05 23:03:00 +000047This is like \code{print_exc(\var{limit})} but returns a string
48instead of printing to a file.
Neal Norwitz378f7b52003-12-13 22:34:09 +000049\versionadded{2.4}
Neil Schemenauerf607fc52003-11-05 23:03:00 +000050\end{funcdesc}
51
Guido van Rossumbca12071998-06-17 22:37:26 +000052\begin{funcdesc}{print_last}{\optional{limit\optional{, file}}}
Fred Drakedb40afa2002-07-25 21:11:23 +000053This is a shorthand for \code{print_exception(sys.last_type,
54sys.last_value, sys.last_traceback, \var{limit}, \var{file})}.
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;
Fred Drakedb40afa2002-07-25 21:11:23 +000097however, for \exception{SyntaxError} exceptions, it contains several
98lines that (when printed) display detailed information about where the
Guido van Rossumbca12071998-06-17 22:37:26 +000099syntax 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
Thomas Woutersf8316632000-07-16 19:01:10 +0000108these lines are concatenated and printed, exactly the same text is
Guido van Rossumbca12071998-06-17 22:37:26 +0000109printed 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
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000122object. This function was necessary because in versions of Python
Fred Drake008a36a2003-01-30 22:22:59 +0000123prior to 2.3 when the \programopt{-O} flag was passed to Python the
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000124\code{\var{tb}.tb_lineno} was not updated correctly. This function
125has no use in versions past 2.3.
Guido van Rossumbca12071998-06-17 22:37:26 +0000126\end{funcdesc}
127
Fred Drake31d10cb1999-06-29 17:08:41 +0000128
129\subsection{Traceback Example \label{traceback-example}}
130
131This simple example implements a basic read-eval-print loop, similar
132to (but less useful than) the standard Python interactive interpreter
133loop. For a more complete implementation of the interpreter loop,
134refer to the \refmodule{code} module.
Guido van Rossumbca12071998-06-17 22:37:26 +0000135
136\begin{verbatim}
137import sys, traceback
138
139def run_user_code(envdir):
140 source = raw_input(">>> ")
141 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000142 exec(source, envdir)
Guido van Rossumbca12071998-06-17 22:37:26 +0000143 except:
144 print "Exception in user code:"
Guido van Rossumfaac0131998-06-17 22:38:09 +0000145 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000146 traceback.print_exc(file=sys.stdout)
Guido van Rossumfaac0131998-06-17 22:38:09 +0000147 print '-'*60
Guido van Rossumbca12071998-06-17 22:37:26 +0000148
149envdir = {}
150while 1:
151 run_user_code(envdir)
152\end{verbatim}