diff options
| author | Brett Cannon <bcannon@gmail.com> | 2006-12-13 23:09:53 (GMT) |
|---|---|---|
| committer | Brett Cannon <bcannon@gmail.com> | 2006-12-13 23:09:53 (GMT) |
| commit | 6d9520c4f0e01ff87ff3d74166f508df0a215261 (patch) | |
| tree | 4024486982a7efc6717ead6db1e0e44105bb27b6 /Lib/test/test_support.py | |
| parent | c745df8519b3dce95bb3ce7587c4c311f8184290 (diff) | |
| download | cpython-6d9520c4f0e01ff87ff3d74166f508df0a215261.zip cpython-6d9520c4f0e01ff87ff3d74166f508df0a215261.tar.gz cpython-6d9520c4f0e01ff87ff3d74166f508df0a215261.tar.bz2 | |
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.
Diffstat (limited to 'Lib/test/test_support.py')
| -rw-r--r-- | Lib/test/test_support.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index ae39aa1..4939410 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -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 |
