exceptions, internals (introspection?)

ej ejatwellkeepercom
Thu Nov 10 15:29:57 EST 2005


I'm trying to figure out how to get at some of the internal interpreter
state for exception handlers and debug statements in general.
I just read through Section 8 of the Python Tutorial. I see how to catch an
exception, and specify an optional argument to get ahold of the exception
itself. For example:

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


    If you don't handle an exception, the interpreter will quit and print a
stack trace. What I'm not seeing is how to handle the exception, but still
get the stack trace as a string so I could dump it to a log file or email it
or something.

    I have often wondered how to get at other internals, such as the name of
the current function, file, line number I am in?  The arguments to the
current function, etc.  I browsed through the table of contents of both the
Library Reference & Language Reference. I see section 18. Python Language
Services.  In browsing through that, I'm thinking "Oh man... this is way
more than I need - there's got to be an easier way."   Nothing else is
jumping out at me. Can someone point me to some documentation on this
subject and/or provide some examples?

Thanks,  :)
-ej





More information about the Python-list mailing list