Commit ccd62705 authored by Victor Stinner's avatar Victor Stinner

Issue #23375: Fix test_py3kwarn for modules implemented in C

Don't check if importing a module emits a DeprecationWarning if the module is
implemented in C and the module is already loaded.
parent cc1db4bf
...@@ -2,6 +2,7 @@ import unittest ...@@ -2,6 +2,7 @@ import unittest
import sys import sys
from test.test_support import check_py3k_warnings, CleanImport, run_unittest from test.test_support import check_py3k_warnings, CleanImport, run_unittest
import warnings import warnings
from test import test_support
if not sys.py3kwarning: if not sys.py3kwarning:
raise unittest.SkipTest('%s must be run with the -3 flag' % __name__) raise unittest.SkipTest('%s must be run with the -3 flag' % __name__)
...@@ -356,6 +357,21 @@ class TestStdlibRemovals(unittest.TestCase): ...@@ -356,6 +357,21 @@ class TestStdlibRemovals(unittest.TestCase):
def check_removal(self, module_name, optional=False): def check_removal(self, module_name, optional=False):
"""Make sure the specified module, when imported, raises a """Make sure the specified module, when imported, raises a
DeprecationWarning and specifies itself in the message.""" DeprecationWarning and specifies itself in the message."""
if module_name in sys.modules:
mod = sys.modules[module_name]
filename = getattr(mod, '__file__', '')
mod = None
# the module is not implemented in C?
if not filename.endswith(('.py', '.pyc', '.pyo')):
# Issue #23375: If the module was already loaded, reimporting
# the module will not emit again the warning. The warning is
# emited when the module is loaded, but C modules cannot
# unloaded.
if test_support.verbose:
print("Cannot test the Python 3 DeprecationWarning of the "
"%s module, the C module is already loaded"
% module_name)
return
with CleanImport(module_name), warnings.catch_warnings(): with CleanImport(module_name), warnings.catch_warnings():
warnings.filterwarnings("error", ".+ (module|package) .+ removed", warnings.filterwarnings("error", ".+ (module|package) .+ removed",
DeprecationWarning, __name__) DeprecationWarning, __name__)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment