lISTINg 12-1:
Discovering the default action and installed filters
# Import the required modules.
import warnings
# Display the default action.
print ‘Default action:’, warnings.default_action
# Display the default filters.
print ‘\nDefault Filters:’
for filter in warnings.filters:
print ‘Action:’, filter[0],
print ‘Msg:’, filter[1],
print ‘Cat:’, str(filter[2]).split(“‘“)[1].split(‘.’)[1],
print ‘Module:’, filter[3],
print ‘Line:’, filter[4]
# Add new filters.
warnings.filterwarnings(‘always’, message=’Test’, category=UserWarning)
warnings.filterwarnings(‘always’, message=’Test2’, category=UserWarning,
module=’Test’)
warnings.filterwarnings(‘always’, message=’Test3’, category=UserWarning,
module=’Test’, append=True)
# Display the updated filters.
print ‘\nUpdated Filters:’
for filter in warnings.filters:
print ‘Action:’, filter[0],
try:
print ‘Msg:’, filter[1].pattern,
except AttributeError:
print ‘None’,
print ‘Cat:’, str(filter[2]).split(“‘“)[1].split(‘.’)[1],
try:
if len(filter[3].pattern) == 0:
print ‘Module: Undefined’,
else:
print ‘Module:’, filter[3].pattern,
except AttributeError:
548592c12.indd 246
2/24/10 12:48:46 PM
www.finebook.ir
Understanding IronPython Warnings
❘
247
print ‘Module: None’,
print ‘Line:’, filter[4]
# Pause after the debug session.
raw_input(‘\nPress any key to continue...’)
The code begins by importing the
warnings
module. It then displays (using
warnings.default_
action
) the default action that the interpreter will take when it encounters a warning. As shown in
Figure 12-4 and described in Table 12-2, the default action is
‘default‘
.
FIgURE 12-4:
The example shows the default actions and filters, along with the output of filter changes.
Because of space limitations in this book, the text of Listing 12-1
shows two lines of code as being split, when they should appear on a
single line. You would type the
warnings.filterwarnings(‘always‘,
message=‘Test2‘, category=UserWarning, module=‘Test‘)
and
warnings.filterwarnings(‘always‘, message=‘Test3‘,
category=UserWarning, module=‘Test‘, append=True)
lines of code
on a single line. The example won’t work if you type the code on multiple
lines as shown.
The next step is to show the default filters that the interpreter provides for you. It may surprise you
to know that the interpreter does include some default filters for the
PendingDeprecationWarning
,
ImportWarning
, and
BytesWarning
, as shown in Figure 12-4. These default filters make the interpreter
easier and more enjoyable to use, but could also hide important bugs, so you need to be aware of them.
In order to show how actions and filters work, the example adds three filters using the
warnings
.filterwarnings()
function. The first filter simply tells the interpreter to always display warnings
about the
Test
message provided in the
UserWarning
category. The second filter specifies that the
Test2
warning will appear in the
Test
module. The third filter specifies that the interpreter should
548592c12.indd 247
2/24/10 12:48:46 PM
www.finebook.ir
Do'stlaringiz bilan baham: |