diff options
Diffstat (limited to 'Lib/test/test_warnings.py')
| -rw-r--r-- | Lib/test/test_warnings.py | 127 | 
1 files changed, 93 insertions, 34 deletions
| diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index 65a1a28..dbf30e9 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -4,7 +4,6 @@ import os  from io import StringIO  import sys  import unittest -import tempfile  import subprocess  from test import support  from test.script_helper import assert_python_ok @@ -508,7 +507,7 @@ class _WarningsTests(BaseTest):              with support.captured_output('stderr') as stream:                  self.module.warn(text)                  result = stream.getvalue() -        self.assertTrue(text in result) +        self.assertIn(text, result)      def test_showwarning_not_callable(self):          with original_warnings.catch_warnings(module=self.module): @@ -669,18 +668,33 @@ class CatchWarningTests(BaseTest):      def test_check_warnings(self):          # Explicit tests for the test.support convenience wrapper          wmod = self.module -        if wmod is sys.modules['warnings']: -            with support.check_warnings() as w: -                self.assertEqual(w.warnings, []) -                wmod.simplefilter("always") +        if wmod is not sys.modules['warnings']: +            return +        with support.check_warnings(quiet=False) as w: +            self.assertEqual(w.warnings, []) +            wmod.simplefilter("always") +            wmod.warn("foo") +            self.assertEqual(str(w.message), "foo") +            wmod.warn("bar") +            self.assertEqual(str(w.message), "bar") +            self.assertEqual(str(w.warnings[0].message), "foo") +            self.assertEqual(str(w.warnings[1].message), "bar") +            w.reset() +            self.assertEqual(w.warnings, []) + +        with support.check_warnings(): +            # defaults to quiet=True without argument +            pass +        with support.check_warnings(('foo', UserWarning)): +            wmod.warn("foo") + +        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") -                self.assertEqual(str(w.message), "foo") -                wmod.warn("bar") -                self.assertEqual(str(w.message), "bar") -                self.assertEqual(str(w.warnings[0].message), "foo") -                self.assertEqual(str(w.warnings[1].message), "bar") -                w.reset() -                self.assertEqual(w.warnings, [])  class CCatchWarningTests(CatchWarningTests):      module = c_warnings @@ -689,32 +703,76 @@ class PyCatchWarningTests(CatchWarningTests):      module = py_warnings +class EnvironmentVariableTests(BaseTest): + +    def test_single_warning(self): +        newenv = os.environ.copy() +        newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning" +        p = subprocess.Popen([sys.executable, +                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], +                stdout=subprocess.PIPE, env=newenv) +        self.assertEqual(p.communicate()[0], b"['ignore::DeprecationWarning']") +        self.assertEqual(p.wait(), 0) + +    def test_comma_separated_warnings(self): +        newenv = os.environ.copy() +        newenv["PYTHONWARNINGS"] = ("ignore::DeprecationWarning," +                                    "ignore::UnicodeWarning") +        p = subprocess.Popen([sys.executable, +                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], +                stdout=subprocess.PIPE, env=newenv) +        self.assertEqual(p.communicate()[0], +                b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']") +        self.assertEqual(p.wait(), 0) + +    def test_envvar_and_command_line(self): +        newenv = os.environ.copy() +        newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning" +        p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning", +                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], +                stdout=subprocess.PIPE, env=newenv) +        self.assertEqual(p.communicate()[0], +                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): +    module = c_warnings + +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 -        cwd = tempfile.mkdtemp() -        try: -            encodings = os.path.join(cwd, 'encodings') -            os.mkdir(encodings) -            try: -                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) -            finally: -                os.rmdir(encodings) -        finally: -            os.rmdir(cwd) +        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() @@ -726,6 +784,7 @@ def test_main():          _WarningsTests,          CWarningsDisplayTests, PyWarningsDisplayTests,          CCatchWarningTests, PyCatchWarningTests, +        CEnvironmentVariableTests, PyEnvironmentVariableTests,          BootstrapTest,      ) | 
