Manually printing traceback?

Jeff Shannon jeff at ccvcorp.com
Fri Aug 10 12:51:21 EDT 2001


Andrei Kulakov wrote:

> Hello,
>
> If I run this code: l = []; print l[1], I get a traceback.
> If I change it to:
>
> import sys
> l = []
> try: print l[1]
> except:
>     print sys.exc_info()
>
> I get a tuple, 3rd element of which is the traceback object. How do i print
> it? I tried doing

> >>>print sys.exc_info()[2]()  ....

Your question has already been answered, but I thought I'd point this out.

The reason that your above attempt isn't working, is you've got an exra ()
there.  If:

>>> e = sys.exc_info()    # returns a tuple

>>> print e   # prints the full tuple, equivalent to your first except: clause

>>> print e[2]   # prints only the third element of that tuple

What you were trying to do is equivalent to

>>> print e[2]()  # attempts to use the third element as a callable object
(function), and then print the result.

This is unlikely to work, as I doubt that traceback objects are callable.  :)
You *could*, however,

>>> print sys.exc_info()[2]

without the extra parens, and that should work as you expected.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list