rest25/library/traceback.rst => rest262/library/traceback.rst
132   This function returns the current line number set in the traceback object.  This
133   function was necessary because in versions of Python prior to 2.3 when the
134   :option:`-O` flag was passed to Python the ``tb.tb_lineno`` was not updated
135   correctly.  This function has no use in versions past 2.3.
136
137
138.. _traceback-example:
139
n140-Traceback Example
n140+Traceback Examples
141------------------
141+------------------
142
143This simple example implements a basic read-eval-print loop, similar to (but
144less useful than) the standard Python interactive interpreter loop.  For a more
145complete implementation of the interpreter loop, refer to the :mod:`code`
146module. ::
147
148   import sys, traceback
149
156           print '-'*60
157           traceback.print_exc(file=sys.stdout)
158           print '-'*60
159
160   envdir = {}
161   while 1:
162       run_user_code(envdir)
163
t164+ 
165+The following example demonstrates the different ways to print and format the
166+exception and traceback::
167+ 
168+   import sys, traceback
169+ 
170+   def lumberjack():
171+       bright_side_of_death()
172+ 
173+   def bright_side_of_death():
174+       return tuple()[0]
175+ 
176+   try:
177+       lumberjack()
178+   except:
179+       exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
180+       print "*** print_tb:"
181+       traceback.print_tb(exceptionTraceback, limit=1, file=sys.stdout)
182+       print "*** print_exception:"
183+       traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
184+                                 limit=2, file=sys.stdout)
185+       print "*** print_exc:"
186+       traceback.print_exc()
187+       print "*** format_exc, first and last line:"
188+       formatted_lines = traceback.format_exc().splitlines()
189+       print formatted_lines[0]
190+       print formatted_lines[-1]
191+       print "*** format_exception:"
192+       print repr(traceback.format_exception(exceptionType, exceptionValue,
193+                                             exceptionTraceback))
194+       print "*** extract_tb:"
195+       print repr(traceback.extract_tb(exceptionTraceback))
196+       print "*** format_tb:"
197+       print repr(traceback.format_tb(exceptionTraceback))
198+       print "*** tb_lineno:", traceback.tb_lineno(exceptionTraceback)
199+   print "*** print_last:"
200+   traceback.print_last()
201+ 
202+ 
203+The output for the example would look similar to this::
204+ 
205+   *** print_tb:
206+     File "<doctest>", line 9, in <module>
207+       lumberjack()
208+   *** print_exception:
209+   Traceback (most recent call last):
210+     File "<doctest>", line 9, in <module>
211+       lumberjack()
212+     File "<doctest>", line 3, in lumberjack
213+       bright_side_of_death()
214+   IndexError: tuple index out of range
215+   *** print_exc:
216+   Traceback (most recent call last):
217+     File "<doctest>", line 9, in <module>
218+       lumberjack()
219+     File "<doctest>", line 3, in lumberjack
220+       bright_side_of_death()
221+   IndexError: tuple index out of range
222+   *** format_exc, first and last line:
223+   Traceback (most recent call last):
224+   IndexError: tuple index out of range
225+   *** format_exception:
226+   ['Traceback (most recent call last):\n',
227+    '  File "<doctest>", line 9, in <module>\n    lumberjack()\n',
228+    '  File "<doctest>", line 3, in lumberjack\n    bright_side_of_death()\n',
229+    '  File "<doctest>", line 6, in bright_side_of_death\n    return tuple()[0]\n',
230+    'IndexError: tuple index out of range\n']
231+   *** extract_tb:
232+   [('<doctest>', 9, '<module>', 'lumberjack()'),
233+    ('<doctest>', 3, 'lumberjack', 'bright_side_of_death()'),
234+    ('<doctest>', 6, 'bright_side_of_death', 'return tuple()[0]')]
235+   *** format_tb:
236+   ['  File "<doctest>", line 9, in <module>\n    lumberjack()\n',
237+    '  File "<doctest>", line 3, in lumberjack\n    bright_side_of_death()\n',
238+    '  File "<doctest>", line 6, in bright_side_of_death\n    return tuple()[0]\n']
239+   *** tb_lineno: 2
240+   *** print_last:
241+   Traceback (most recent call last):
242+     File "<doctest>", line 9, in <module>
243+       lumberjack()
244+     File "<doctest>", line 3, in lumberjack
245+       bright_side_of_death()
246+   IndexError: tuple index out of range
247+ 
248+ 
249+The following example shows the different ways to print and format the stack::
250+ 
251+   >>> import traceback
252+   >>> def another_function():
253+   ...     lumberstack()
254+   ...
255+   >>> def lumberstack():
256+   ...     traceback.print_stack()
257+   ...     print repr(traceback.extract_stack())
258+   ...     print repr(traceback.format_stack())
259+   ...
260+   >>> another_function()
261+     File "<doctest>", line 10, in <module>
262+       another_function()
263+     File "<doctest>", line 3, in another_function
264+       lumberstack()
265+     File "<doctest>", line 6, in lumberstack
266+       traceback.print_stack()
267+   [('<doctest>', 10, '<module>', 'another_function()'),
268+    ('<doctest>', 3, 'another_function', 'lumberstack()'),
269+    ('<doctest>', 7, 'lumberstack', 'print repr(traceback.extract_stack())')]
270+   ['  File "<doctest>", line 10, in <module>\n    another_function()\n',
271+    '  File "<doctest>", line 3, in another_function\n    lumberstack()\n',
272+    '  File "<doctest>", line 8, in lumberstack\n    print repr(traceback.format_stack())\n']
273+ 
274+ 
275+This last example demonstrates the final few formatting functions::
276+ 
277+   >>> import traceback
278+   >>> format_list([('spam.py', 3, '<module>', 'spam.eggs()'),
279+   ...              ('eggs.py', 42, 'eggs', 'return "bacon"')])
280+   ['  File "spam.py", line 3, in <module>\n    spam.eggs()\n',
281+    '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
282+   >>> theError = IndexError('tuple indx out of range')
283+   >>> traceback.format_exception_only(type(theError), theError)
284+   ['IndexError: tuple index out of range\n']
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op