exceptions, internals (introspection?)

ej ejatwellkeepercom
Thu Nov 10 17:11:52 EST 2005


"Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message
news:7x64r04473.fsf at ruckus.brouhaha.com...
> It's messy.  Look at sys.exc_info() and go from there.

Yeah, I think I am starting to see what you mean...


#! /usr/local/bin/python

import sys

try:
    {}['foo']
except Exception, x:
    print "class of x =", x.__class__
    print "type(x) =", type(x)
    print "dir(x) =", dir(x)

    print
    (type_, value_, traceback_) = sys.exc_info()
    print "type_ =", type_
    print "value_ =", value_
    print "traceback_ =", traceback_
    for key in dir(traceback_):
        print "traceback_.%s =" % key, eval("traceback_.%s" % key)

    print "dir(frame) = ", dir(traceback_.tb_frame)


ej at sand:~/src/python/exceptions> foo
class of x = exceptions.KeyError
type(x) = <type 'instance'>
dir(x) = ['__doc__', '__getitem__', '__init__', '__module__', '__str__',
'args']

type_ = exceptions.KeyError
value_ = 'foo'
traceback_ = <traceback object at 0x402e2e14>
traceback_.tb_frame = <frame object at 0x8177b3c>
traceback_.tb_lasti = 18
traceback_.tb_lineno = 6
traceback_.tb_next = None
dir(frame) =  ['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'f_back', 'f_builtins', 'f_code',
'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti',
'f_lineno', 'f_locals', 'f_restricted', 'f_trace']



But at least that is something to go on. Thanks for your reply!

-ej





More information about the Python-list mailing list