Skip to content

Commit 8362893

Browse files
authored
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.
1 parent 8e34e92 commit 8362893

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

Lib/distutils/tests/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
import os
1616
import sys
1717
import unittest
18-
import warnings
1918
from test.support import run_unittest
19+
from test.support.warnings_helper import save_restore_warnings_filters
2020

2121

2222
here = os.path.dirname(__file__) or os.curdir
2323

2424

2525
def test_suite():
26-
old_filters = warnings.filters[:]
2726
suite = unittest.TestSuite()
2827
for fn in os.listdir(here):
2928
if fn.startswith("test") and fn.endswith(".py"):
3029
modname = "distutils.tests." + fn[:-3]
31-
__import__(modname)
30+
# bpo-40055: Save/restore warnings filters to leave them unchanged.
31+
# Importing tests imports docutils which imports pkg_resources
32+
# which adds a warnings filter.
33+
with save_restore_warnings_filters():
34+
__import__(modname)
3235
module = sys.modules[modname]
3336
suite.addTest(module.test_suite())
34-
# bpo-40055: Save/restore warnings filters to leave them unchanged.
35-
# Importing tests imports docutils which imports pkg_resources which adds a
36-
# warnings filter.
37-
warnings.filters[:] = old_filters
3837
return suite
3938

4039

Lib/test/pickletester.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,26 @@
2121
except ImportError:
2222
_testbuffer = None
2323

24-
try:
25-
import numpy as np
26-
except ImportError:
27-
np = None
28-
2924
from test import support
3025
from test.support import (
3126
TestFailed, TESTFN, run_with_locale, no_tracing,
3227
_2G, _4G, bigmemtest, forget,
3328
)
3429
from test.support import threading_helper
30+
from test.support.warnings_helper import save_restore_warnings_filters
3531

3632
from pickle import bytes_types
3733

34+
35+
# bpo-41003: Save/restore warnings filters to leave them unchanged.
36+
# Ignore filters installed by numpy.
37+
try:
38+
with save_restore_warnings_filters():
39+
import numpy as np
40+
except ImportError:
41+
np = None
42+
43+
3844
requires_32b = unittest.skipUnless(sys.maxsize < 2**32,
3945
"test is only meaningful on 32-bit builds")
4046

Lib/test/support/warnings_helper.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,12 @@ def _filterwarnings(filters, quiet=False):
178178
if missing:
179179
raise AssertionError("filter (%r, %s) did not catch any warning" %
180180
missing[0])
181+
182+
183+
@contextlib.contextmanager
184+
def save_restore_warnings_filters():
185+
old_filters = warnings.filters[:]
186+
try:
187+
yield
188+
finally:
189+
warnings.filters[:] = old_filters
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``test_copyreg`` when ``numpy`` is installed: ``test.pickletester`` now
2+
saves/restores warnings filters when importing ``numpy``, to ignore filters
3+
installed by ``numpy``.

0 commit comments

Comments
 (0)