diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-17 16:07:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-17 16:07:13 (GMT) |
commit | 8362893e3fe083df2ec8bb94c28b1a78383eadbf (patch) | |
tree | c485a614b45419ebbd2b648de6e8e2efa2279515 | |
parent | 8e34e92caa73259620dd242b92d26edd0949b4ba (diff) | |
download | cpython-8362893e3fe083df2ec8bb94c28b1a78383eadbf.zip cpython-8362893e3fe083df2ec8bb94c28b1a78383eadbf.tar.gz cpython-8362893e3fe083df2ec8bb94c28b1a78383eadbf.tar.bz2 |
bpo-41003: Fix test_copyreg when numpy is installed (GH-20935)
Fix test_copyreg when numpy is installed: test.pickletester now
saves/restores warnings.filters when importing numpy, to ignore
filters installed by numpy.
Add the save_restore_warnings_filters() function to the
test.support.warnings_helper module.
-rw-r--r-- | Lib/distutils/tests/__init__.py | 13 | ||||
-rw-r--r-- | Lib/test/pickletester.py | 16 | ||||
-rw-r--r-- | Lib/test/support/warnings_helper.py | 9 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Tests/2020-06-17-15-07-14.bpo-41003.tiH_Fy.rst | 3 |
4 files changed, 29 insertions, 12 deletions
diff --git a/Lib/distutils/tests/__init__.py b/Lib/distutils/tests/__init__.py index 5d2e69e..16d011f 100644 --- a/Lib/distutils/tests/__init__.py +++ b/Lib/distutils/tests/__init__.py @@ -15,26 +15,25 @@ by import rather than matching pre-defined names. import os import sys import unittest -import warnings from test.support import run_unittest +from test.support.warnings_helper import save_restore_warnings_filters here = os.path.dirname(__file__) or os.curdir def test_suite(): - old_filters = warnings.filters[:] suite = unittest.TestSuite() for fn in os.listdir(here): if fn.startswith("test") and fn.endswith(".py"): modname = "distutils.tests." + fn[:-3] - __import__(modname) + # bpo-40055: Save/restore warnings filters to leave them unchanged. + # Importing tests imports docutils which imports pkg_resources + # which adds a warnings filter. + with save_restore_warnings_filters(): + __import__(modname) module = sys.modules[modname] suite.addTest(module.test_suite()) - # bpo-40055: Save/restore warnings filters to leave them unchanged. - # Importing tests imports docutils which imports pkg_resources which adds a - # warnings filter. - warnings.filters[:] = old_filters return suite diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index ca566a2..a34505a 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -21,20 +21,26 @@ try: except ImportError: _testbuffer = None -try: - import numpy as np -except ImportError: - np = None - from test import support from test.support import ( TestFailed, TESTFN, run_with_locale, no_tracing, _2G, _4G, bigmemtest, forget, ) from test.support import threading_helper +from test.support.warnings_helper import save_restore_warnings_filters from pickle import bytes_types + +# bpo-41003: Save/restore warnings filters to leave them unchanged. +# Ignore filters installed by numpy. +try: + with save_restore_warnings_filters(): + import numpy as np +except ImportError: + np = None + + requires_32b = unittest.skipUnless(sys.maxsize < 2**32, "test is only meaningful on 32-bit builds") diff --git a/Lib/test/support/warnings_helper.py b/Lib/test/support/warnings_helper.py index c9f9045..de23e6b 100644 --- a/Lib/test/support/warnings_helper.py +++ b/Lib/test/support/warnings_helper.py @@ -178,3 +178,12 @@ def _filterwarnings(filters, quiet=False): if missing: raise AssertionError("filter (%r, %s) did not catch any warning" % missing[0]) + + +@contextlib.contextmanager +def save_restore_warnings_filters(): + old_filters = warnings.filters[:] + try: + yield + finally: + warnings.filters[:] = old_filters diff --git a/Misc/NEWS.d/next/Tests/2020-06-17-15-07-14.bpo-41003.tiH_Fy.rst b/Misc/NEWS.d/next/Tests/2020-06-17-15-07-14.bpo-41003.tiH_Fy.rst new file mode 100644 index 0000000..6f908d9 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2020-06-17-15-07-14.bpo-41003.tiH_Fy.rst @@ -0,0 +1,3 @@ +Fix ``test_copyreg`` when ``numpy`` is installed: ``test.pickletester`` now +saves/restores warnings filters when importing ``numpy``, to ignore filters +installed by ``numpy``. |