Add test.test_support.guard_warnings_filter . This function returns a context

manager that protects warnings.filter from being modified once the context is
exited.
This commit is contained in:
Brett Cannon 2006-12-13 23:09:53 +00:00
parent c745df8519
commit 6d9520c4f0
6 changed files with 37 additions and 26 deletions

View file

@ -3,7 +3,9 @@
if __name__ != 'test.test_support':
raise ImportError, 'test_support must be imported from the test package'
from contextlib import contextmanager
import sys
import warnings
class Error(Exception):
"""Base class for regression test exceptions."""
@ -268,6 +270,16 @@ def open_urlresource(url):
print >> get_original_stdout(), '\tfetching %s ...' % url
fn, _ = urllib.urlretrieve(url, filename)
return open(fn)
@contextmanager
def guard_warnings_filter():
"""Guard the warnings filter from being permanently changed."""
original_filters = warnings.filters[:]
try:
yield
finally:
warnings.filters = original_filters
#=======================================================================
# Decorator for running a function in a different locale, correctly resetting