Skip to content

Commit b39d41b

Browse files
authored
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 8362893)
1 parent 2c6d6c1 commit b39d41b

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

Lib/distutils/tests/__init__.py

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

2120

2221
here = os.path.dirname(__file__) or os.curdir
2322

2423

2524
def test_suite():
26-
old_filters = warnings.filters[:]
2725
suite = unittest.TestSuite()
2826
for fn in os.listdir(here):
2927
if fn.startswith("test") and fn.endswith(".py"):
3028
modname = "distutils.tests." + fn[:-3]
31-
__import__(modname)
29+
# bpo-40055: Save/restore warnings filters to leave them unchanged.
30+
# Importing tests imports docutils which imports pkg_resources
31+
# which adds a warnings filter.
32+
with save_restore_warnings_filters():
33+
__import__(modname)
3234
module = sys.modules[modname]
3335
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
3836
return suite
3937

4038

Lib/test/pickletester.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@
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, reap_threads, forget,
28+
save_restore_warnings_filters
3329
)
3430

3531
from pickle import bytes_types
3632

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

Lib/test/support/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,3 +3159,12 @@ def use_old_parser():
31593159

31603160
def skip_if_new_parser(msg):
31613161
return unittest.skipIf(not use_old_parser(), msg)
3162+
3163+
3164+
@contextlib.contextmanager
3165+
def save_restore_warnings_filters():
3166+
old_filters = warnings.filters[:]
3167+
try:
3168+
yield
3169+
finally:
3170+
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)