Skip to content

[3.9] bpo-41003: Fix test_copyreg when numpy is installed (GH-20935) #20945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Lib/distutils/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@
import os
import sys
import unittest
import warnings
from test.support import run_unittest
from test.support import run_unittest, 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


Expand Down
16 changes: 11 additions & 5 deletions Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3159,3 +3159,12 @@ def use_old_parser():

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
Original file line number Diff line number Diff line change
@@ -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``.