254
❘
CHAPTER 12
Debugging ironPython APPlicAtions
low-level functions in the
sys
module. The following list is low-level modules you can use for special
needs, but won’t normally use in your application.
➤➤
sys.excepthook(
type
,
value
,
traceback
)
: The system calls this low-level function each
time it generates an exception. To use this function, you supply the same tuple of values as
you receive when you call
sys.exc_info()
.
➤
➤
sys._getframe([
depth
])
: The system calls this low-level function to display a frame
object from the call stack. If the caller supplies a
depth
value, the frame object is at that
call stack depth. The default
depth
value setting is 0. IronPython doesn’t appear to imple-
ment this function, but you may encounter it in other versions of Python, so it pays to
know about this function.
If you want to control how much information the interpreter provides when you request a
traceback
,
you can always set the
sys.tracebacklimit
attribute. The
sys.tracebacklimit
attribute defaults to
1,000. It doesn’t actually appear when you perform a
dir()
command. In fact, until you set it, printing
the
sys.tracebacklimit
attribute returns an
AttributeError
. Use code like this
sys.tracebacklimit = 3
to modify the
traceback
level. Now when you try to print the
sys.tracebacklimit
attribute, you
get back the value you supplied.
Using the traceback Module
The
traceback
module adds to the capabilities of the
sys
module described in the “Using the sys
Module” section of the chapter. In addition, it adds to the standard exception handling capabilities
of IronPython by making it easier to obtain complex information about exceptions in general. The
traceback
module does focus on
traceback
s, which are the IronPython equivalent of a call stack.
The most common call is
traceback.print_exc()
. Essentially, this call prints out the current
exception information. You can use it in a
try...except
structure, much as you’d use the
sys.exc_info()
function, but with fewer limitations. Figure 12-6 shows a typical view of
the
traceback.print_exc()
function in action.
FIgURE 12-6:
Obtain traceback information with ease using the traceback.print_exc() function.
548592c12.indd 254
2/24/10 12:48:48 PM
www.finebook.ir
Do'stlaringiz bilan baham: |