mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-41003: Fix test_copyreg when numpy is installed (GH-20935) (GH-20945)
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.
(cherry picked from commit 8362893e3f
)
This commit is contained in:
parent
2c6d6c12c2
commit
b39d41ba1b
4 changed files with 29 additions and 13 deletions
|
@ -15,26 +15,24 @@ by import rather than matching pre-defined names.
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
from test.support import run_unittest, save_restore_warnings_filters
|
||||||
from test.support import run_unittest
|
|
||||||
|
|
||||||
|
|
||||||
here = os.path.dirname(__file__) or os.curdir
|
here = os.path.dirname(__file__) or os.curdir
|
||||||
|
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
old_filters = warnings.filters[:]
|
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
for fn in os.listdir(here):
|
for fn in os.listdir(here):
|
||||||
if fn.startswith("test") and fn.endswith(".py"):
|
if fn.startswith("test") and fn.endswith(".py"):
|
||||||
modname = "distutils.tests." + fn[:-3]
|
modname = "distutils.tests." + fn[:-3]
|
||||||
|
# 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)
|
__import__(modname)
|
||||||
module = sys.modules[modname]
|
module = sys.modules[modname]
|
||||||
suite.addTest(module.test_suite())
|
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
|
return suite
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -21,19 +21,25 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
_testbuffer = None
|
_testbuffer = None
|
||||||
|
|
||||||
try:
|
|
||||||
import numpy as np
|
|
||||||
except ImportError:
|
|
||||||
np = None
|
|
||||||
|
|
||||||
from test import support
|
from test import support
|
||||||
from test.support import (
|
from test.support import (
|
||||||
TestFailed, TESTFN, run_with_locale, no_tracing,
|
TestFailed, TESTFN, run_with_locale, no_tracing,
|
||||||
_2G, _4G, bigmemtest, reap_threads, forget,
|
_2G, _4G, bigmemtest, reap_threads, forget,
|
||||||
|
save_restore_warnings_filters
|
||||||
)
|
)
|
||||||
|
|
||||||
from pickle import bytes_types
|
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,
|
requires_32b = unittest.skipUnless(sys.maxsize < 2**32,
|
||||||
"test is only meaningful on 32-bit builds")
|
"test is only meaningful on 32-bit builds")
|
||||||
|
|
||||||
|
|
|
@ -3159,3 +3159,12 @@ def use_old_parser():
|
||||||
|
|
||||||
def skip_if_new_parser(msg):
|
def skip_if_new_parser(msg):
|
||||||
return unittest.skipIf(not use_old_parser(), msg)
|
return unittest.skipIf(not use_old_parser(), msg)
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def save_restore_warnings_filters():
|
||||||
|
old_filters = warnings.filters[:]
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
warnings.filters[:] = old_filters
|
||||||
|
|
|
@ -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``.
|
Loading…
Add table
Add a link
Reference in a new issue