diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -35,17 +35,17 @@ def warnings_state(module): module.simplefilter("once") warning_tests.warnings = module yield finally: warning_tests.warnings = original_warnings module.filters = original_filters -class BaseTest(unittest.TestCase): +class BaseTest: """Basic bookkeeping required for testing.""" def setUp(self): # The __warningregistry__ needs to be in a pristine state for tests # to work properly. if '__warningregistry__' in globals(): del globals()['__warningregistry__'] @@ -58,17 +58,17 @@ class BaseTest(unittest.TestCase): sys.modules['warnings'] = self.module super(BaseTest, self).setUp() def tearDown(self): sys.modules['warnings'] = original_warnings super(BaseTest, self).tearDown() -class FilterTests(object): +class FilterTests(BaseTest): """Testing the filtering functionality.""" def test_error(self): with original_warnings.catch_warnings(module=self.module) as w: self.module.resetwarnings() self.module.filterwarnings("error", category=UserWarning) self.assertRaises(UserWarning, self.module.warn, @@ -181,24 +181,24 @@ class FilterTests(object): self.module.resetwarnings() self.module.filterwarnings("error", "hex*", Warning, "", 0) self.assertRaises(UserWarning, self.module.warn, 'hex/oct') text = 'nonmatching text' self.module.warn(text) self.assertEqual(str(w[-1].message), text) self.assertTrue(w[-1].category is UserWarning) -class CFilterTests(BaseTest, FilterTests): +class CFilterTests(FilterTests, unittest.TestCase): module = c_warnings -class PyFilterTests(BaseTest, FilterTests): +class PyFilterTests(FilterTests, unittest.TestCase): module = py_warnings -class WarnTests(unittest.TestCase): +class WarnTests: """Test warnings.warn() and warnings.warn_explicit().""" def test_message(self): with original_warnings.catch_warnings(record=True, module=self.module) as w: self.module.simplefilter("once") for i in range(4): @@ -355,36 +355,36 @@ class WarnTests(unittest.TestCase): def __str__(self): return ("A bad formatted string %(err)" % {"err" : "there is no %(err)s"}) with self.assertRaises(ValueError): self.module.warn(BadStrWarning()) -class CWarnTests(BaseTest, WarnTests): +class CWarnTests(BaseTest, WarnTests, unittest.TestCase): module = c_warnings # As an early adopter, we sanity check the # test.support.import_fresh_module utility function def test_accelerated(self): self.assertFalse(original_warnings is self.module) self.assertFalse(hasattr(self.module.warn, '__code__')) -class PyWarnTests(BaseTest, WarnTests): +class PyWarnTests(BaseTest, WarnTests, unittest.TestCase): module = py_warnings # As an early adopter, we sanity check the # test.support.import_fresh_module utility function def test_pure_python(self): self.assertFalse(original_warnings is self.module) self.assertTrue(hasattr(self.module.warn, '__code__')) -class WCmdLineTests(unittest.TestCase): +class WCmdLineTests: def test_improper_input(self): # Uses the private _setoption() function to test the parsing # of command-line warning arguments with original_warnings.catch_warnings(module=self.module): self.assertRaises(self.module._OptionError, self.module._setoption, '1:2:3:4:5:6') self.assertRaises(self.module._OptionError, @@ -405,24 +405,24 @@ class WCmdLineTests(unittest.TestCase): # Check that the warnings module does get loaded when -W # is used (see issue #10372 for an example of silent bootstrap failure). rc, out, err = assert_python_ok("-Wi", "-c", "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)") # '-Wi' was observed self.assertFalse(out.strip()) self.assertNotIn(b'RuntimeWarning', err) -class CWCmdLineTests(BaseTest, WCmdLineTests): +class CWCmdLineTests(BaseTest, WCmdLineTests, unittest.TestCase): module = c_warnings -class PyWCmdLineTests(BaseTest, WCmdLineTests): +class PyWCmdLineTests(BaseTest, WCmdLineTests, unittest.TestCase): module = py_warnings -class _WarningsTests(BaseTest): +class _WarningsTests(BaseTest, unittest.TestCase): """Tests specific to the _warnings module.""" module = c_warnings def test_filter(self): # Everything should function even if 'filters' is not in warnings. with original_warnings.catch_warnings(module=self.module) as w: @@ -552,17 +552,17 @@ class _WarningsTests(BaseTest): self.module.filterwarnings("always", category=UserWarning) globals_dict['__file__'] = None original_warnings.warn('test', UserWarning) self.assertTrue(len(w)) finally: globals_dict['__file__'] = oldfile -class WarningsDisplayTests(unittest.TestCase): +class WarningsDisplayTests: """Test the displaying of warnings and the ability to overload functions related to displaying warnings.""" def test_formatwarning(self): message = "msg" category = Warning file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' @@ -596,20 +596,20 @@ class WarningsDisplayTests(unittest.Test expected_file_line += "for the win!" expect = self.module.formatwarning(message, category, file_name, line_num, expected_file_line) file_object = StringIO() self.module.showwarning(message, category, file_name, line_num, file_object, expected_file_line) self.assertEqual(expect, file_object.getvalue()) -class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): +class CWarningsDisplayTests(BaseTest, WarningsDisplayTests, unittest.TestCase): module = c_warnings -class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): +class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests, unittest.TestCase): module = py_warnings class CatchWarningTests(BaseTest): """Test catch_warnings().""" def test_catch_warnings_restore(self): @@ -705,20 +705,20 @@ class CatchWarningTests(BaseTest): with self.assertRaises(AssertionError): with support.check_warnings(('', RuntimeWarning)): # defaults to quiet=False with argument pass with self.assertRaises(AssertionError): with support.check_warnings(('foo', RuntimeWarning)): wmod.warn("foo") -class CCatchWarningTests(CatchWarningTests): +class CCatchWarningTests(CatchWarningTests, unittest.TestCase): module = c_warnings -class PyCatchWarningTests(CatchWarningTests): +class PyCatchWarningTests(CatchWarningTests, unittest.TestCase): module = py_warnings class EnvironmentVariableTests(BaseTest): def test_single_warning(self): newenv = os.environ.copy() newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning" @@ -757,20 +757,20 @@ class EnvironmentVariableTests(BaseTest) newenv["PYTHONIOENCODING"] = "utf-8" p = subprocess.Popen([sys.executable, "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], stdout=subprocess.PIPE, env=newenv) self.assertEqual(p.communicate()[0], "['ignore:DeprecaciónWarning']".encode('utf-8')) self.assertEqual(p.wait(), 0) -class CEnvironmentVariableTests(EnvironmentVariableTests): +class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase): module = c_warnings -class PyEnvironmentVariableTests(EnvironmentVariableTests): +class PyEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase): module = py_warnings class BootstrapTest(unittest.TestCase): def test_issue_8766(self): # "import encodings" emits a warning whereas the warnings is not loaded # or not completely loaded (warnings imports indirectly encodings by # importing linecache) yet @@ -783,25 +783,10 @@ class BootstrapTest(unittest.TestCase): self.assertEqual(retcode, 0) # Use -W to load warnings module at startup retcode = subprocess.call( [sys.executable, '-c', 'pass', '-W', 'always'], env=env) self.assertEqual(retcode, 0) -def test_main(): - py_warnings.onceregistry.clear() - c_warnings.onceregistry.clear() - support.run_unittest( - CFilterTests, PyFilterTests, - CWarnTests, PyWarnTests, - CWCmdLineTests, PyWCmdLineTests, - _WarningsTests, - CWarningsDisplayTests, PyWarningsDisplayTests, - CCatchWarningTests, PyCatchWarningTests, - CEnvironmentVariableTests, PyEnvironmentVariableTests, - BootstrapTest, - ) - - if __name__ == "__main__": - test_main() + unittest.main()