|
32 | 32 | import signal
|
33 | 33 | import stat
|
34 | 34 | import tempfile
|
| 35 | +import warnings |
| 36 | +from contextlib import contextmanager |
35 | 37 |
|
36 | 38 | from sympy.core.cache import clear_cache
|
37 | 39 | from sympy.core.compatibility import (exec_, PY3, unwrap,
|
38 | 40 | unicode)
|
39 | 41 | from sympy.utilities.misc import find_executable
|
40 | 42 | from sympy.external import import_module
|
41 |
| -from sympy.utilities.exceptions import SymPyDeprecationWarning |
42 | 43 |
|
43 | 44 | IS_WINDOWS = (os.name == 'nt')
|
44 | 45 | ON_TRAVIS = os.getenv('TRAVIS_BUILD_NUMBER', None)
|
@@ -156,6 +157,21 @@ def setup_pprint():
|
156 | 157 | interactive_printing.NO_GLOBAL = True
|
157 | 158 | return use_unicode_prev
|
158 | 159 |
|
| 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 | + |
159 | 175 | def run_in_subprocess_with_hash_randomization(
|
160 | 176 | function, function_args=(),
|
161 | 177 | function_kwargs=None, command=sys.executable,
|
@@ -537,11 +553,6 @@ def _test(*paths, **kwargs):
|
537 | 553 | fast_threshold=fast_threshold,
|
538 | 554 | slow_threshold=slow_threshold)
|
539 | 555 |
|
540 |
| - # Show deprecation warnings |
541 |
| - import warnings |
542 |
| - warnings.simplefilter("error", SymPyDeprecationWarning) |
543 |
| - warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*') |
544 |
| - |
545 | 556 | test_files = t.get_test_files('sympy')
|
546 | 557 |
|
547 | 558 | not_blacklisted = [f for f in test_files
|
@@ -786,11 +797,6 @@ def _doctest(*paths, **kwargs):
|
786 | 797 | from sympy.plotting.plot import unset_show
|
787 | 798 | unset_show()
|
788 | 799 |
|
789 |
| - # Show deprecation warnings |
790 |
| - import warnings |
791 |
| - warnings.simplefilter("error", SymPyDeprecationWarning) |
792 |
| - warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*') |
793 |
| - |
794 | 800 | r = PyTestReporter(verbose, split=split, colors=colors,\
|
795 | 801 | force_colors=force_colors)
|
796 | 802 | t = SymPyDocTests(r, normal)
|
@@ -1278,11 +1284,12 @@ def test_file(self, filename, sort=True, timeout=False, slow=False,
|
1278 | 1284 | try:
|
1279 | 1285 | if getattr(f, '_slow', False) and not slow:
|
1280 | 1286 | 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() |
1286 | 1293 | except KeyboardInterrupt:
|
1287 | 1294 | if getattr(f, '_slow', False):
|
1288 | 1295 | reporter.test_skip("KeyboardInterrupt")
|
@@ -1828,15 +1835,17 @@ def run(self, test, compileflags=None, out=None, clear_globs=True):
|
1828 | 1835 | self.save_linecache_getlines = pdoctest.linecache.getlines
|
1829 | 1836 | linecache.getlines = self.__patched_linecache_getlines
|
1830 | 1837 |
|
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() |
1840 | 1849 |
|
1841 | 1850 |
|
1842 | 1851 | # We have to override the name mangled methods.
|
|
0 commit comments