mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
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:
parent
c745df8519
commit
6d9520c4f0
6 changed files with 37 additions and 26 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue