blob: ec8687fc7eb956310a71eaeb0413bac6b4730c50 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`traceback` --- Print or retrieve a stack traceback
3========================================================
4
5.. module:: traceback
6 :synopsis: Print or retrieve a stack traceback.
7
8
9This module provides a standard interface to extract, format and print stack
10traces of Python programs. It exactly mimics the behavior of the Python
11interpreter when it prints a stack trace. This is useful when you want to print
12stack traces under program control, such as in a "wrapper" around the
13interpreter.
14
15.. index:: object: traceback
16
17The module uses traceback objects --- this is the object type that is stored in
18the ``sys.last_traceback`` variable and returned as the third item from
19:func:`sys.exc_info`.
20
21The module defines the following functions:
22
23
24.. function:: print_tb(traceback[, limit[, file]])
25
26 Print up to *limit* stack trace entries from *traceback*. If *limit* is omitted
27 or ``None``, all entries are printed. If *file* is omitted or ``None``, the
28 output goes to ``sys.stderr``; otherwise it should be an open file or file-like
29 object to receive the output.
30
31
32.. function:: print_exception(type, value, traceback[, limit[, file]])
33
34 Print exception information and up to *limit* stack trace entries from
35 *traceback* to *file*. This differs from :func:`print_tb` in the following ways:
36 (1) if *traceback* is not ``None``, it prints a header ``Traceback (most recent
37 call last):``; (2) it prints the exception *type* and *value* after the stack
38 trace; (3) if *type* is :exc:`SyntaxError` and *value* has the appropriate
39 format, it prints the line where the syntax error occurred with a caret
40 indicating the approximate position of the error.
41
42
43.. function:: print_exc([limit[, file]])
44
45 This is a shorthand for ``print_exception(*sys.exc_info()``.
46
47
48.. function:: format_exc([limit])
49
50 This is like ``print_exc(limit)`` but returns a string instead of printing to a
51 file.
52
53 .. versionadded:: 2.4
54
55
56.. function:: print_last([limit[, file]])
57
58 This is a shorthand for ``print_exception(sys.last_type, sys.last_value,
59 sys.last_traceback, limit, file)``.
60
61
62.. function:: print_stack([f[, limit[, file]]])
63
64 This function prints a stack trace from its invocation point. The optional *f*
65 argument can be used to specify an alternate stack frame to start. The optional
66 *limit* and *file* arguments have the same meaning as for
67 :func:`print_exception`.
68
69
70.. function:: extract_tb(traceback[, limit])
71
72 Return a list of up to *limit* "pre-processed" stack trace entries extracted
73 from the traceback object *traceback*. It is useful for alternate formatting of
74 stack traces. If *limit* is omitted or ``None``, all entries are extracted. A
75 "pre-processed" stack trace entry is a quadruple (*filename*, *line number*,
76 *function name*, *text*) representing the information that is usually printed
77 for a stack trace. The *text* is a string with leading and trailing whitespace
78 stripped; if the source is not available it is ``None``.
79
80
81.. function:: extract_stack([f[, limit]])
82
83 Extract the raw traceback from the current stack frame. The return value has
84 the same format as for :func:`extract_tb`. The optional *f* and *limit*
85 arguments have the same meaning as for :func:`print_stack`.
86
87
88.. function:: format_list(list)
89
90 Given a list of tuples as returned by :func:`extract_tb` or
91 :func:`extract_stack`, return a list of strings ready for printing. Each string
92 in the resulting list corresponds to the item with the same index in the
93 argument list. Each string ends in a newline; the strings may contain internal
94 newlines as well, for those items whose source text line is not ``None``.
95
96
97.. function:: format_exception_only(type, value)
98
99 Format the exception part of a traceback. The arguments are the exception type
100 and value such as given by ``sys.last_type`` and ``sys.last_value``. The return
101 value is a list of strings, each ending in a newline. Normally, the list
102 contains a single string; however, for :exc:`SyntaxError` exceptions, it
103 contains several lines that (when printed) display detailed information about
104 where the syntax error occurred. The message indicating which exception
105 occurred is the always last string in the list.
106
107
108.. function:: format_exception(type, value, tb[, limit])
109
110 Format a stack trace and the exception information. The arguments have the
111 same meaning as the corresponding arguments to :func:`print_exception`. The
112 return value is a list of strings, each ending in a newline and some containing
113 internal newlines. When these lines are concatenated and printed, exactly the
114 same text is printed as does :func:`print_exception`.
115
116
117.. function:: format_tb(tb[, limit])
118
119 A shorthand for ``format_list(extract_tb(tb, limit))``.
120
121
122.. function:: format_stack([f[, limit]])
123
124 A shorthand for ``format_list(extract_stack(f, limit))``.
125
126
127.. function:: tb_lineno(tb)
128
129 This function returns the current line number set in the traceback object. This
130 function was necessary because in versions of Python prior to 2.3 when the
131 :option:`-O` flag was passed to Python the ``tb.tb_lineno`` was not updated
132 correctly. This function has no use in versions past 2.3.
133
134
135.. _traceback-example:
136
137Traceback Example
138-----------------
139
140This simple example implements a basic read-eval-print loop, similar to (but
141less useful than) the standard Python interactive interpreter loop. For a more
142complete implementation of the interpreter loop, refer to the :mod:`code`
143module. ::
144
145 import sys, traceback
146
147 def run_user_code(envdir):
148 source = raw_input(">>> ")
149 try:
150 exec(source, envdir)
151 except:
152 print "Exception in user code:"
153 print '-'*60
154 traceback.print_exc(file=sys.stdout)
155 print '-'*60
156
157 envdir = {}
158 while 1:
159 run_user_code(envdir)
160