diff options
Diffstat (limited to 'Lib/test/test_warnings.py')
| -rw-r--r-- | Lib/test/test_warnings.py | 112 |
1 files changed, 78 insertions, 34 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index e502ed8..79be835 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -1,19 +1,19 @@ from contextlib import contextmanager import linecache import os -import StringIO +from io import StringIO import sys import unittest import subprocess -from test import test_support +from test import support from test.script_helper import assert_python_ok -import warning_tests +from test import warning_tests import warnings as original_warnings -py_warnings = test_support.import_fresh_module('warnings', blocked=['_warnings']) -c_warnings = test_support.import_fresh_module('warnings', fresh=['_warnings']) +py_warnings = support.import_fresh_module('warnings', blocked=['_warnings']) +c_warnings = support.import_fresh_module('warnings', fresh=['_warnings']) @contextmanager def warnings_state(module): @@ -99,7 +99,7 @@ class FilterTests(object): self.module.resetwarnings() self.module.filterwarnings("default", category=UserWarning) message = UserWarning("FilterTests.test_default") - for x in xrange(2): + for x in range(2): self.module.warn(message, UserWarning) if x == 0: self.assertEqual(w[-1].message, message) @@ -207,6 +207,18 @@ class WarnTests(unittest.TestCase): self.assertEqual(str(w[-1].message), text) self.assertTrue(w[-1].category is UserWarning) + # Issue 3639 + def test_warn_nonstandard_types(self): + # warn() should handle non-standard types without issue. + for ob in (Warning, None, 42): + with original_warnings.catch_warnings(record=True, + module=self.module) as w: + self.module.simplefilter("once") + self.module.warn(ob) + # Don't directly compare objects since + # ``Warning() != Warning()``. + self.assertEqual(str(w[-1].message), str(UserWarning(ob))) + def test_filename(self): with warnings_state(self.module): with original_warnings.catch_warnings(record=True, @@ -352,19 +364,19 @@ class CWarnTests(BaseTest, WarnTests): module = c_warnings # As an early adopter, we sanity check the - # test_support.import_fresh_module utility function + # test.support.import_fresh_module utility function def test_accelerated(self): self.assertFalse(original_warnings is self.module) - self.assertFalse(hasattr(self.module.warn, 'func_code')) + self.assertFalse(hasattr(self.module.warn, '__code__')) class PyWarnTests(BaseTest, WarnTests): module = py_warnings # As an early adopter, we sanity check the - # test_support.import_fresh_module utility function + # 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, 'func_code')) + self.assertTrue(hasattr(self.module.warn, '__code__')) class WCmdLineTests(unittest.TestCase): @@ -492,7 +504,7 @@ class _WarningsTests(BaseTest): with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning - with test_support.captured_output('stderr') as stream: + with support.captured_output('stderr') as stream: self.module.warn(text) result = stream.getvalue() self.assertIn(text, result) @@ -513,7 +525,7 @@ class _WarningsTests(BaseTest): with original_warnings.catch_warnings(module=self.module): self.module.filterwarnings("always", category=UserWarning) del self.module.showwarning - with test_support.captured_output('stderr') as stream: + with support.captured_output('stderr') as stream: warning_tests.inner(text) result = stream.getvalue() self.assertEqual(result.count('\n'), 2, @@ -538,7 +550,7 @@ class _WarningsTests(BaseTest): with original_warnings.catch_warnings(module=self.module) as w: self.module.filterwarnings("always", category=UserWarning) globals_dict['__file__'] = None - self.module.warn('test', UserWarning) + original_warnings.warn('test', UserWarning) finally: globals_dict['__file__'] = oldfile @@ -572,7 +584,7 @@ class WarningsDisplayTests(unittest.TestCase): expected_file_line = linecache.getline(file_name, line_num).strip() message = 'msg' category = Warning - file_object = StringIO.StringIO() + file_object = StringIO() expect = self.module.formatwarning(message, category, file_name, line_num) self.module.showwarning(message, category, file_name, line_num, @@ -582,7 +594,7 @@ class WarningsDisplayTests(unittest.TestCase): expected_file_line += "for the win!" expect = self.module.formatwarning(message, category, file_name, line_num, expected_file_line) - file_object = StringIO.StringIO() + file_object = StringIO() self.module.showwarning(message, category, file_name, line_num, file_object, expected_file_line) self.assertEqual(expect, file_object.getvalue()) @@ -666,11 +678,11 @@ class CatchWarningTests(BaseTest): self.assertTrue(wmod.filters is orig_filters) def test_check_warnings(self): - # Explicit tests for the test_support convenience wrapper + # Explicit tests for the test.support convenience wrapper wmod = self.module if wmod is not sys.modules['warnings']: return - with test_support.check_warnings(quiet=False) as w: + with support.check_warnings(quiet=False) as w: self.assertEqual(w.warnings, []) wmod.simplefilter("always") wmod.warn("foo") @@ -682,21 +694,20 @@ class CatchWarningTests(BaseTest): w.reset() self.assertEqual(w.warnings, []) - with test_support.check_warnings(): + with support.check_warnings(): # defaults to quiet=True without argument pass - with test_support.check_warnings(('foo', UserWarning)): + with support.check_warnings(('foo', UserWarning)): wmod.warn("foo") with self.assertRaises(AssertionError): - with test_support.check_warnings(('', RuntimeWarning)): + with support.check_warnings(('', RuntimeWarning)): # defaults to quiet=False with argument pass with self.assertRaises(AssertionError): - with test_support.check_warnings(('foo', RuntimeWarning)): + with support.check_warnings(('foo', RuntimeWarning)): wmod.warn("foo") - class CCatchWarningTests(CatchWarningTests): module = c_warnings @@ -712,7 +723,7 @@ class EnvironmentVariableTests(BaseTest): 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::DeprecationWarning']") + self.assertEqual(p.communicate()[0], b"['ignore::DeprecationWarning']") self.assertEqual(p.wait(), 0) def test_comma_separated_warnings(self): @@ -723,7 +734,7 @@ class EnvironmentVariableTests(BaseTest): "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], stdout=subprocess.PIPE, env=newenv) self.assertEqual(p.communicate()[0], - "['ignore::DeprecationWarning', 'ignore::UnicodeWarning']") + b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']") self.assertEqual(p.wait(), 0) def test_envvar_and_command_line(self): @@ -733,7 +744,20 @@ class EnvironmentVariableTests(BaseTest): "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], stdout=subprocess.PIPE, env=newenv) self.assertEqual(p.communicate()[0], - "['ignore::UnicodeWarning', 'ignore::DeprecationWarning']") + b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']") + self.assertEqual(p.wait(), 0) + + @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii', + 'requires non-ascii filesystemencoding') + def test_nonascii(self): + newenv = os.environ.copy() + newenv["PYTHONWARNINGS"] = "ignore:DeprecaciónWarning" + 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): @@ -743,18 +767,38 @@ class PyEnvironmentVariableTests(EnvironmentVariableTests): 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 + with support.temp_cwd() as cwd, support.temp_cwd('encodings'): + env = os.environ.copy() + env['PYTHONPATH'] = cwd + + # encodings loaded by initfsencoding() + retcode = subprocess.call([sys.executable, '-c', 'pass'], env=env) + 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() - test_support.run_unittest(CFilterTests, PyFilterTests, - CWarnTests, PyWarnTests, - CWCmdLineTests, PyWCmdLineTests, - _WarningsTests, - CWarningsDisplayTests, PyWarningsDisplayTests, - CCatchWarningTests, PyCatchWarningTests, - CEnvironmentVariableTests, - PyEnvironmentVariableTests - ) + support.run_unittest( + CFilterTests, PyFilterTests, + CWarnTests, PyWarnTests, + CWCmdLineTests, PyWCmdLineTests, + _WarningsTests, + CWarningsDisplayTests, PyWarningsDisplayTests, + CCatchWarningTests, PyCatchWarningTests, + CEnvironmentVariableTests, PyEnvironmentVariableTests, + BootstrapTest, + ) if __name__ == "__main__": |
