252
❘
CHAPTER 12
Debugging ironPython APPlicAtions
The warning categories are used with almost every
warnings
module function. For example, you
supply a category when setting a filter or creating a new message. There is always an exception. The
resetwarnings()
function doesn’t require any input, not even a warning category, because it resets
the entire warning environment to a default state.
obTAININg ERRoR INFoRMATIoN
Errors will happen in your application, even if you use good exception handling. The handlers you
create only react to the errors you know about. Applications also encounter unknown errors. In this
case, your application has to have a way to obtain error information and display it to the user (or at
least record it in a log file).
It’s important to remember that you normally obtain error information in an application using the
exception process described in the “Defining and Using Exceptions” section of this chapter. This
section of the chapter is more designed for those situations where you need to work with a generic
exception or obtain more detailed information than the specific exceptions provide.
As with many things, IronPython provides a number of methods for obtaining error information.
In fact, you might be surprised at how many ways you can retrieve information once you really start
looking. The following sections discuss the most common methods for obtaining error information.
Using the sys Module
The
sys
module contains a wealth of useful functions and attributes you use to obtain, track,
and manage error information. One of the first things you should know about the
sys
module is
that it contains the
sys.stderr
attribute, which defines where the interpreter sends error output.
Normally, the output goes to the console window, but you can redirect the error output to any
object that has a
write()
method associated with it, such as a file. If you want to later reset the
sys.stderr
attribute to the console, the
sys.__stderr__
attribute always contains the original
output location, so using
sys.stderr = sys.__stderr__
performs a reset.
Obtaining error information seems like it should be straightforward, but it’s harder than most develop-
ers initially think because obtaining error information often affects application execution in unforeseen
ways. In addition, ensuring that the caller receives the right information in a multithreaded application
is difficult. The caller could also make unfortunate changes to error information objects, such as the
traceback
object, creating problems with circular references that the garbage collector is unable to
handle. Consequently, you find a lot of functions in
sys
that look like they should do something use-
ful (and this section covers them), but the two functions you need to keep in mind when working with
IronPython are.
➤➤
Do'stlaringiz bilan baham: |