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
|
@ -263,6 +263,10 @@ If no match is found \var{filename} is returned.
|
||||||
This does not equal a failure since it could be the path to the file.
|
This does not equal a failure since it could be the path to the file.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
|
\begin{funcdesc}{guard_warnings_filter}{}
|
||||||
|
Returns a context manager that guards the \module{warnings} module's
|
||||||
|
filter settings.
|
||||||
|
|
||||||
\begin{funcdesc}{run_unittest}{*classes}
|
\begin{funcdesc}{run_unittest}{*classes}
|
||||||
Execute \class{unittest.TestCase} subclasses passed to the function.
|
Execute \class{unittest.TestCase} subclasses passed to the function.
|
||||||
The function scans the classes for methods starting with the prefix
|
The function scans the classes for methods starting with the prefix
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from test.test_support import TESTFN, run_unittest
|
from test.test_support import TESTFN, run_unittest, guard_warnings_filter
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
import py_compile
|
import py_compile
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
def remove_files(name):
|
def remove_files(name):
|
||||||
|
@ -204,15 +205,11 @@ class ImportTest(unittest.TestCase):
|
||||||
self.assert_(y is test.test_support, y.__name__)
|
self.assert_(y is test.test_support, y.__name__)
|
||||||
|
|
||||||
def test_import_initless_directory_warning(self):
|
def test_import_initless_directory_warning(self):
|
||||||
import warnings
|
with guard_warnings_filter():
|
||||||
oldfilters = warnings.filters[:]
|
|
||||||
warnings.simplefilter('error', ImportWarning);
|
|
||||||
try:
|
|
||||||
# Just a random non-package directory we always expect to be
|
# Just a random non-package directory we always expect to be
|
||||||
# somewhere in sys.path...
|
# somewhere in sys.path...
|
||||||
|
warnings.simplefilter('error', ImportWarning)
|
||||||
self.assertRaises(ImportWarning, __import__, "site-packages")
|
self.assertRaises(ImportWarning, __import__, "site-packages")
|
||||||
finally:
|
|
||||||
warnings.filters = oldfilters
|
|
||||||
|
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
run_unittest(ImportTest)
|
run_unittest(ImportTest)
|
||||||
|
|
|
@ -178,10 +178,9 @@ class WichmannHill_TestBasicOps(TestBasicOps):
|
||||||
|
|
||||||
def test_bigrand(self):
|
def test_bigrand(self):
|
||||||
# Verify warnings are raised when randrange is too large for random()
|
# Verify warnings are raised when randrange is too large for random()
|
||||||
oldfilters = warnings.filters[:]
|
with test_support.guard_warnings_filter():
|
||||||
warnings.filterwarnings("error", "Underlying random")
|
warnings.filterwarnings("error", "Underlying random")
|
||||||
self.assertRaises(UserWarning, self.gen.randrange, 2**60)
|
self.assertRaises(UserWarning, self.gen.randrange, 2**60)
|
||||||
warnings.filters[:] = oldfilters
|
|
||||||
|
|
||||||
class SystemRandom_TestBasicOps(TestBasicOps):
|
class SystemRandom_TestBasicOps(TestBasicOps):
|
||||||
gen = random.SystemRandom()
|
gen = random.SystemRandom()
|
||||||
|
|
|
@ -50,9 +50,7 @@ def any_err(func, *args):
|
||||||
|
|
||||||
def with_warning_restore(func):
|
def with_warning_restore(func):
|
||||||
def _with_warning_restore(*args, **kw):
|
def _with_warning_restore(*args, **kw):
|
||||||
# The `warnings` module doesn't have an advertised way to restore
|
with test.test_support.guard_warnings_filter():
|
||||||
# its filter list. Cheat.
|
|
||||||
save_warnings_filters = warnings.filters[:]
|
|
||||||
# Grrr, we need this function to warn every time. Without removing
|
# Grrr, we need this function to warn every time. Without removing
|
||||||
# the warningregistry, running test_tarfile then test_struct would fail
|
# the warningregistry, running test_tarfile then test_struct would fail
|
||||||
# on 64-bit platforms.
|
# on 64-bit platforms.
|
||||||
|
@ -62,10 +60,7 @@ def with_warning_restore(func):
|
||||||
warnings.filterwarnings("error", r"""^struct.*""", DeprecationWarning)
|
warnings.filterwarnings("error", r"""^struct.*""", DeprecationWarning)
|
||||||
warnings.filterwarnings("error", r""".*format requires.*""",
|
warnings.filterwarnings("error", r""".*format requires.*""",
|
||||||
DeprecationWarning)
|
DeprecationWarning)
|
||||||
try:
|
|
||||||
return func(*args, **kw)
|
return func(*args, **kw)
|
||||||
finally:
|
|
||||||
warnings.filters[:] = save_warnings_filters[:]
|
|
||||||
return _with_warning_restore
|
return _with_warning_restore
|
||||||
|
|
||||||
def deprecated_err(func, *args):
|
def deprecated_err(func, *args):
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
if __name__ != 'test.test_support':
|
if __name__ != 'test.test_support':
|
||||||
raise ImportError, 'test_support must be imported from the test package'
|
raise ImportError, 'test_support must be imported from the test package'
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
import sys
|
import sys
|
||||||
|
import warnings
|
||||||
|
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
"""Base class for regression test exceptions."""
|
"""Base class for regression test exceptions."""
|
||||||
|
@ -269,6 +271,16 @@ def open_urlresource(url):
|
||||||
fn, _ = urllib.urlretrieve(url, filename)
|
fn, _ = urllib.urlretrieve(url, filename)
|
||||||
return open(fn)
|
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
|
# Decorator for running a function in a different locale, correctly resetting
|
||||||
# it afterwards.
|
# it afterwards.
|
||||||
|
|
|
@ -302,6 +302,10 @@ Extension Modules
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Added guard_warnings_filter to test.test_support. It returns a context
|
||||||
|
manager that protects the 'warnings' module's filter from being mutated
|
||||||
|
once the context has been exited.
|
||||||
|
|
||||||
- Added some tests for modulefinder.
|
- Added some tests for modulefinder.
|
||||||
|
|
||||||
- Converted test_imp to use unittest.
|
- Converted test_imp to use unittest.
|
||||||
|
|
Loading…
Reference in New Issue