@@ -14,7 +14,7 @@ interpreter.
1414.. index :: object: traceback
1515
1616The module uses traceback objects --- this is the object type that is stored in
17- the `` sys.last_traceback ` ` variable and returned as the third item from
17+ the :data: ` sys.last_traceback ` variable and returned as the third item from
1818:func: `sys.exc_info `.
1919
2020The module defines the following functions:
@@ -55,7 +55,8 @@ The module defines the following functions:
5555.. function :: print_last([limit[, file[, chain]]])
5656
5757 This is a shorthand for ``print_exception(sys.last_type, sys.last_value,
58- sys.last_traceback, limit, file) ``.
58+ sys.last_traceback, limit, file) ``. In general it will work only after
59+ an exception has reached an interactive prompt (see :data: `sys.last_type `).
5960
6061
6162.. function :: print_stack([f[, limit[, file]]])
@@ -157,7 +158,9 @@ module. ::
157158
158159
159160The following example demonstrates the different ways to print and format the
160- exception and traceback::
161+ exception and traceback:
162+
163+ .. testcode ::
161164
162165 import sys, traceback
163166
@@ -190,54 +193,47 @@ exception and traceback::
190193 print("*** format_tb:")
191194 print(repr(traceback.format_tb(exceptionTraceback)))
192195 print("* ** tb_lineno:", traceback.tb_lineno(exceptionTraceback))
193- print("*** print_last:")
194- traceback.print_last()
195196
197+ The output for the example would look similar to this:
196198
197- The output for the example would look similar to this::
199+ .. testoutput ::
200+ :options: +NORMALIZE_WHITESPACE
198201
199202 *** print_tb:
200- File "<doctest>", line 9 , in <module>
203+ File "<doctest... >", line 10 , in <module>
201204 lumberjack()
202205 * ** print_exception:
203206 Traceback (most recent call last):
204- File "<doctest>", line 9 , in <module>
207+ File "<doctest... >", line 10 , in <module>
205208 lumberjack()
206- File "<doctest>", line 3 , in lumberjack
209+ File "<doctest... >", line 4 , in lumberjack
207210 bright_side_of_death()
208211 IndexError: tuple index out of range
209212 *** print_exc:
210213 Traceback (most recent call last):
211- File "<doctest>", line 9 , in <module>
214+ File "<doctest... >", line 10 , in <module>
212215 lumberjack()
213- File "<doctest>", line 3 , in lumberjack
216+ File "<doctest... >", line 4 , in lumberjack
214217 bright_side_of_death()
215218 IndexError: tuple index out of range
216219 * ** format_exc, first and last line:
217220 Traceback (most recent call last):
218221 IndexError: tuple index out of range
219222 *** format_exception:
220223 ['Traceback (most recent call last):\n ',
221- ' File "<doctest>", line 9 , in <module>\n lumberjack()\n',
222- ' File "<doctest>", line 3 , in lumberjack\n bright_side_of_death()\n',
223- ' File "<doctest>", line 6 , in bright_side_of_death\n return tuple()[0]\n',
224+ ' File "<doctest... >", line 10 , in <module>\n lumberjack()\n ',
225+ ' File "<doctest... >", line 4 , in lumberjack\n bright_side_of_death()\n ',
226+ ' File "<doctest... >", line 7 , in bright_side_of_death\n return tuple()[0]\n ',
224227 'IndexError: tuple index out of range\n ']
225228 * ** extract_tb:
226- [('<doctest>', 9 , '<module>', 'lumberjack()'),
227- ('<doctest>', 3 , 'lumberjack', 'bright_side_of_death()'),
228- ('<doctest>', 6 , 'bright_side_of_death', 'return tuple()[0]')]
229+ [('<doctest... >', 10 , '<module>', 'lumberjack()'),
230+ ('<doctest... >', 4 , 'lumberjack', 'bright_side_of_death()'),
231+ (u '<doctest... >', 7 , 'bright_side_of_death', 'return tuple()[0]')]
229232 *** format_tb:
230- [' File "<doctest>", line 9, in <module>\n lumberjack()\n',
231- ' File "<doctest>", line 3, in lumberjack\n bright_side_of_death()\n',
232- ' File "<doctest>", line 6, in bright_side_of_death\n return tuple()[0]\n']
233- *** tb_lineno: 2
234- *** print_last:
235- Traceback (most recent call last):
236- File "<doctest>", line 9, in <module>
237- lumberjack()
238- File "<doctest>", line 3, in lumberjack
239- bright_side_of_death()
240- IndexError: tuple index out of range
233+ [' File "<doctest...>", line 10, in <module>\n lumberjack()\n ',
234+ ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n ',
235+ ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n ']
236+ * ** tb_lineno: 10
241237
242238
243239The following example shows the different ways to print and format the stack::
@@ -266,7 +262,10 @@ The following example shows the different ways to print and format the stack::
266262 ' File "<doctest>", line 8, in lumberstack\n print(repr(traceback.format_stack()))\n']
267263
268264
269- This last example demonstrates the final few formatting functions::
265+ This last example demonstrates the final few formatting functions:
266+
267+ .. doctest ::
268+ :options: +NORMALIZE_WHITESPACE
270269
271270 >>> import traceback
272271 >>> traceback.format_list([(' spam.py' , 3 , ' <module>' , ' spam.eggs()' ),
0 commit comments