Skip to content

Commit 5855696

Browse files
committed
fix(testing): Set the warning filter around each test
When running tests/doctests with sympy's own test runners a warning filter is used to turn DeprecationWarning/SymPyDeprecationWarning into an error. The filter was only set one at the start of running the tests. This means that if the filter is modified by one test then it will still be modified when subsequent tests run. This commit adds a raise_on_deprecated context manager in runtests.py and uses it to set the warning filter in the test runners. The filter is set immediately around running the test and is reset before each individual test. This has picked up on a couple of failures that will need to be fixed in subsequent commits.
1 parent bbc9bde commit 5855696

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

sympy/testing/runtests.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
import signal
3333
import stat
3434
import tempfile
35+
import warnings
36+
from contextlib import contextmanager
3537

3638
from sympy.core.cache import clear_cache
3739
from sympy.core.compatibility import (exec_, PY3, unwrap,
3840
unicode)
3941
from sympy.utilities.misc import find_executable
4042
from sympy.external import import_module
41-
from sympy.utilities.exceptions import SymPyDeprecationWarning
4243

4344
IS_WINDOWS = (os.name == 'nt')
4445
ON_TRAVIS = os.getenv('TRAVIS_BUILD_NUMBER', None)
@@ -156,6 +157,21 @@ def setup_pprint():
156157
interactive_printing.NO_GLOBAL = True
157158
return use_unicode_prev
158159

160+
161+
@contextmanager
162+
def raise_on_deprecated():
163+
"""Context manager to make DeprecationWarning raise an error
164+
165+
This is to catch SymPyDeprecationWarning from library code while running
166+
tests and doctests. It is important to use this context manager around
167+
each individual test/doctest in case some tests modify the warning
168+
filters.
169+
"""
170+
with warnings.catch_warnings():
171+
warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*')
172+
yield
173+
174+
159175
def run_in_subprocess_with_hash_randomization(
160176
function, function_args=(),
161177
function_kwargs=None, command=sys.executable,
@@ -537,11 +553,6 @@ def _test(*paths, **kwargs):
537553
fast_threshold=fast_threshold,
538554
slow_threshold=slow_threshold)
539555

540-
# Show deprecation warnings
541-
import warnings
542-
warnings.simplefilter("error", SymPyDeprecationWarning)
543-
warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*')
544-
545556
test_files = t.get_test_files('sympy')
546557

547558
not_blacklisted = [f for f in test_files
@@ -786,11 +797,6 @@ def _doctest(*paths, **kwargs):
786797
from sympy.plotting.plot import unset_show
787798
unset_show()
788799

789-
# Show deprecation warnings
790-
import warnings
791-
warnings.simplefilter("error", SymPyDeprecationWarning)
792-
warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*')
793-
794800
r = PyTestReporter(verbose, split=split, colors=colors,\
795801
force_colors=force_colors)
796802
t = SymPyDocTests(r, normal)
@@ -1278,11 +1284,12 @@ def test_file(self, filename, sort=True, timeout=False, slow=False,
12781284
try:
12791285
if getattr(f, '_slow', False) and not slow:
12801286
raise Skipped("Slow")
1281-
if timeout:
1282-
self._timeout(f, timeout, fail_on_timeout)
1283-
else:
1284-
random.seed(self._seed)
1285-
f()
1287+
with raise_on_deprecated():
1288+
if timeout:
1289+
self._timeout(f, timeout, fail_on_timeout)
1290+
else:
1291+
random.seed(self._seed)
1292+
f()
12861293
except KeyboardInterrupt:
12871294
if getattr(f, '_slow', False):
12881295
reporter.test_skip("KeyboardInterrupt")
@@ -1828,15 +1835,17 @@ def run(self, test, compileflags=None, out=None, clear_globs=True):
18281835
self.save_linecache_getlines = pdoctest.linecache.getlines
18291836
linecache.getlines = self.__patched_linecache_getlines
18301837

1831-
try:
1832-
test.globs['print_function'] = print_function
1833-
return self.__run(test, compileflags, out)
1834-
finally:
1835-
sys.stdout = save_stdout
1836-
pdb.set_trace = save_set_trace
1837-
linecache.getlines = self.save_linecache_getlines
1838-
if clear_globs:
1839-
test.globs.clear()
1838+
# Fail for deprecation warnings
1839+
with raise_on_deprecated():
1840+
try:
1841+
test.globs['print_function'] = print_function
1842+
return self.__run(test, compileflags, out)
1843+
finally:
1844+
sys.stdout = save_stdout
1845+
pdb.set_trace = save_set_trace
1846+
linecache.getlines = self.save_linecache_getlines
1847+
if clear_globs:
1848+
test.globs.clear()
18401849

18411850

18421851
# We have to override the name mangled methods.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy