248
❘
CHAPTER 12
Debugging ironPython APPlicAtions
append the warning filter to the end of the filter list, rather than add it to the front of the list as is
traditional. You can see the result of all three filter additions in Figure 12-4.
The code used to display the filter information is different in this case because the simple display
method used earlier won’t work. What you’ll see as output for the message and module information
is something like
which isn’t particularly useful. In order to get information from the message and module elements,
you must access the
pattern
attribute. Unfortunately, this attribute isn’t available with the default
filters, so the solution is to create a
try...except AttributeError
structure, as shown in the
code. When the code encounters a default filter entry, it simply prints
None
as it would have done
in the past.
Working with modules presents a special problem. If you look at the first filter declaration, it doesn’t
include the
Module
attribute. Unfortunately, the interpreter takes this omission to mean that you
want to create a blank entry, not a null entry. Consequently, the module code also handles the empty
entry scenario by saying the module is undefined. If you want to create a null module entry, you
must use
Module=None
as part of your filter declaration.
Notice in Figure 12-4 that the first two filters appear at the front of the list and in reverse order.
That’s because the interpreter always adds new filters to the beginning of the list unless you
include the
append=True
attribute. Because the third filter includes this attribute, it appears at
the end of the list.
UNdERSTANdINg THE WARNINg TyPE ExTRACTIoN CodE
You’ve probably noticed that the code for extracting the type string from
filter[2]
is somewhat complex. Some extraction sequences can become this way in IronPython.
Let’s start simply. When you
print filter[2]
directly you get
While this output is descriptive, it doesn’t really make type apparent in a friendly
way. Consequently, you can
print str(filter[2]).split(“‘“)
to obtain the
following array with three elements
[‘’]
The next step is to extract the second array element and split the remaining text in
two. You use
print str(filter[2]).split(“‘“)[1].split(‘.‘)
to perform
this task and the output looks like this:
[‘exceptions’, ‘PendingDeprecationWarning’]
The final step is to extract the second array element again and print it to screen. So
the final statement is
print str(filter[2]).split(“‘“)[1].split(‘.‘)[1]
.
Even though this code looks complex, it really isn’t once you take it apart.
548592c12.indd 248
2/24/10 12:48:46 PM
www.finebook.ir
Do'stlaringiz bilan baham: |