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 8362893e3fe083df2ec8bb94c28b1a78383eadbf)
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 6ef4c89..fe5a50b 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -21,19 +21,25 @@
 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, reap_threads, forget,
+    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/__init__.py b/Lib/test/support/__init__.py
index f867014..a847e65 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -3159,3 +3159,12 @@
 
 def skip_if_new_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