exceptions, internals (introspection?)

E

ej

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
 
F

Fredrik Lundh

ej said:
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.

the second example on this page shows you how to do that:

http://effbot.org/librarybook/traceback

</F>
 
P

Paul Rubin

ej said:
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.

It's messy. Look at sys.exc_info() and go from there.
 
P

Paul McNett

ej said:
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.

Others have given you information on how to get at the stack trace. But
regarding getting at some of the other internals you are talking about:

Back to exceptions, you can also provide your own global exception handler by
overriding sys.excepthook (drop in your own function).
 
E

ej

Paul Rubin said:
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@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
 
P

Paul Rubin

ej said:
for key in dir(traceback_):
print "traceback_.%s =" % key, eval("traceback_.%s" % key)

Don't use eval for this. Use getattr(traceback_, key).
traceback_.tb_frame = <frame object at 0x8177b3c>
traceback_.tb_lasti = 18
traceback_.tb_lineno = 6
traceback_.tb_next = None

Yeah. As /F mentioned, there's also a traceback module that parses
this stuff out for you.
 
F

Fernando Perez

ej said:
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?

Poke around IPython, which implements a pretty massive amount of functionality
in this direction. In particular, you want to read OInspect.py and ultraTB.py.

Cheers,

f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top