[Python-checkins] cpython (merge 3.5 -> 3.5): Merge from 3.5.0 branch.

steve.dower python-checkins at python.org
Mon Sep 7 07:38:07 CEST 2015


https://hg.python.org/cpython/rev/2d01ccbc86f8
changeset:   97733:2d01ccbc86f8
branch:      3.5
parent:      97722:fd4bf05b32ba
parent:      97731:7d320c3bf9c6
user:        Steve Dower <steve.dower at microsoft.com>
date:        Sun Sep 06 22:27:42 2015 -0700
summary:
  Merge from 3.5.0 branch.

files:
  Lib/imp.py                                    |    8 +-
  Lib/test/imp_dummy.py                         |    3 +
  Lib/test/test_imp.py                          |   24 +
  Lib/test/test_time.py                         |   13 +
  Lib/test/test_warnings.py                     |  950 ---------
  Lib/test/test_warnings/__init__.py            |  955 ++++++++++
  Lib/test/test_warnings/__main__.py            |    3 +
  Lib/test/test_warnings/data/import_warning.py |    3 +
  Lib/test/test_warnings/data/stacklevel.py     |    0 
  Lib/warnings.py                               |   31 +-
  Lib/webbrowser.py                             |  120 +-
  Misc/NEWS                                     |   14 +-
  Modules/_testmultiphase.c                     |   10 +
  Modules/timemodule.c                          |   16 +-
  Python/_warnings.c                            |   72 +-
  15 files changed, 1144 insertions(+), 1078 deletions(-)


diff --git a/Lib/imp.py b/Lib/imp.py
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -334,6 +334,12 @@
         """
         import importlib.machinery
         loader = importlib.machinery.ExtensionFileLoader(name, path)
-        return loader.load_module()
+
+        # Issue #24748: Skip the sys.modules check in _load_module_shim;
+        # always load new extension
+        spec = importlib.machinery.ModuleSpec(
+            name=name, loader=loader, origin=path)
+        return _load(spec)
+
 else:
     load_dynamic = None
diff --git a/Lib/test/imp_dummy.py b/Lib/test/imp_dummy.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/imp_dummy.py
@@ -0,0 +1,3 @@
+# Fodder for test of issue24748 in test_imp
+
+dummy_name = True
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -3,6 +3,7 @@
 except ImportError:
     _thread = None
 import importlib
+import importlib.util
 import os
 import os.path
 import shutil
@@ -275,6 +276,29 @@
             self.skipTest("found module doesn't appear to be a C extension")
         imp.load_module(name, None, *found[1:])
 
+    @requires_load_dynamic
+    def test_issue24748_load_module_skips_sys_modules_check(self):
+        name = 'test.imp_dummy'
+        try:
+            del sys.modules[name]
+        except KeyError:
+            pass
+        try:
+            module = importlib.import_module(name)
+            spec = importlib.util.find_spec('_testmultiphase')
+            module = imp.load_dynamic(name, spec.origin)
+            self.assertEqual(module.__name__, name)
+            self.assertEqual(module.__spec__.name, name)
+            self.assertEqual(module.__spec__.origin, spec.origin)
+            self.assertRaises(AttributeError, getattr, module, 'dummy_name')
+            self.assertEqual(module.int_const, 1969)
+            self.assertIs(sys.modules[name], module)
+        finally:
+            try:
+                del sys.modules[name]
+            except KeyError:
+                pass
+
     @unittest.skipIf(sys.dont_write_bytecode,
         "test meaningful only when writing bytecode")
     def test_bug7732(self):
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -174,6 +174,19 @@
     def test_strftime_bounding_check(self):
         self._bounds_checking(lambda tup: time.strftime('', tup))
 
+    def test_strftime_format_check(self):
+        # Test that strftime does not crash on invalid format strings
+        # that may trigger a buffer overread. When not triggered,
+        # strftime may succeed or raise ValueError depending on
+        # the platform.
+        for x in [ '', 'A', '%A', '%AA' ]:
+            for y in range(0x0, 0x10):
+                for z in [ '%', 'A%', 'AA%', '%A%', 'A%A%', '%#' ]:
+                    try:
+                        time.strftime(x * y + z)
+                    except ValueError:
+                        pass
+
     def test_default_values_for_zero(self):
         # Make sure that using all zeros uses the proper default
         # values.  No test for daylight savings since strftime() does
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
deleted file mode 100644
--- a/Lib/test/test_warnings.py
+++ /dev/null
@@ -1,950 +0,0 @@
-from contextlib import contextmanager
-import linecache
-import os
-from io import StringIO
-import sys
-import unittest
-from test import support
-from test.support.script_helper import assert_python_ok, assert_python_failure
-
-from test import warning_tests
-
-import warnings as original_warnings
-
-py_warnings = support.import_fresh_module('warnings', blocked=['_warnings'])
-c_warnings = support.import_fresh_module('warnings', fresh=['_warnings'])
-
- at contextmanager
-def warnings_state(module):
-    """Use a specific warnings implementation in warning_tests."""
-    global __warningregistry__
-    for to_clear in (sys, warning_tests):
-        try:
-            to_clear.__warningregistry__.clear()
-        except AttributeError:
-            pass
-    try:
-        __warningregistry__.clear()
-    except NameError:
-        pass
-    original_warnings = warning_tests.warnings
-    original_filters = module.filters
-    try:
-        module.filters = original_filters[:]
-        module.simplefilter("once")
-        warning_tests.warnings = module
-        yield
-    finally:
-        warning_tests.warnings = original_warnings
-        module.filters = original_filters
-
-
-class BaseTest:
-
-    """Basic bookkeeping required for testing."""
-
-    def setUp(self):
-        self.old_unittest_module = unittest.case.warnings
-        # The __warningregistry__ needs to be in a pristine state for tests
-        # to work properly.
-        if '__warningregistry__' in globals():
-            del globals()['__warningregistry__']
-        if hasattr(warning_tests, '__warningregistry__'):
-            del warning_tests.__warningregistry__
-        if hasattr(sys, '__warningregistry__'):
-            del sys.__warningregistry__
-        # The 'warnings' module must be explicitly set so that the proper
-        # interaction between _warnings and 'warnings' can be controlled.
-        sys.modules['warnings'] = self.module
-        # Ensure that unittest.TestCase.assertWarns() uses the same warnings
-        # module than warnings.catch_warnings(). Otherwise,
-        # warnings.catch_warnings() will be unable to remove the added filter.
-        unittest.case.warnings = self.module
-        super(BaseTest, self).setUp()
-
-    def tearDown(self):
-        sys.modules['warnings'] = original_warnings
-        unittest.case.warnings = self.old_unittest_module
-        super(BaseTest, self).tearDown()
-
-class PublicAPITests(BaseTest):
-
-    """Ensures that the correct values are exposed in the
-    public API.
-    """
-
-    def test_module_all_attribute(self):
-        self.assertTrue(hasattr(self.module, '__all__'))
-        target_api = ["warn", "warn_explicit", "showwarning",
-                      "formatwarning", "filterwarnings", "simplefilter",
-                      "resetwarnings", "catch_warnings"]
-        self.assertSetEqual(set(self.module.__all__),
-                            set(target_api))
-
-class CPublicAPITests(PublicAPITests, unittest.TestCase):
-    module = c_warnings
-
-class PyPublicAPITests(PublicAPITests, unittest.TestCase):
-    module = py_warnings
-
-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,
-                                "FilterTests.test_error")
-
-    def test_error_after_default(self):
-        with original_warnings.catch_warnings(module=self.module) as w:
-            self.module.resetwarnings()
-            message = "FilterTests.test_ignore_after_default"
-            def f():
-                self.module.warn(message, UserWarning)
-            f()
-            self.module.filterwarnings("error", category=UserWarning)
-            self.assertRaises(UserWarning, f)
-
-    def test_ignore(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("ignore", category=UserWarning)
-            self.module.warn("FilterTests.test_ignore", UserWarning)
-            self.assertEqual(len(w), 0)
-
-    def test_ignore_after_default(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            message = "FilterTests.test_ignore_after_default"
-            def f():
-                self.module.warn(message, UserWarning)
-            f()
-            self.module.filterwarnings("ignore", category=UserWarning)
-            f()
-            f()
-            self.assertEqual(len(w), 1)
-
-    def test_always(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("always", category=UserWarning)
-            message = "FilterTests.test_always"
-            self.module.warn(message, UserWarning)
-            self.assertTrue(message, w[-1].message)
-            self.module.warn(message, UserWarning)
-            self.assertTrue(w[-1].message, message)
-
-    def test_always_after_default(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            message = "FilterTests.test_always_after_ignore"
-            def f():
-                self.module.warn(message, UserWarning)
-            f()
-            self.assertEqual(len(w), 1)
-            self.assertEqual(w[-1].message.args[0], message)
-            f()
-            self.assertEqual(len(w), 1)
-            self.module.filterwarnings("always", category=UserWarning)
-            f()
-            self.assertEqual(len(w), 2)
-            self.assertEqual(w[-1].message.args[0], message)
-            f()
-            self.assertEqual(len(w), 3)
-            self.assertEqual(w[-1].message.args[0], message)
-
-    def test_default(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("default", category=UserWarning)
-            message = UserWarning("FilterTests.test_default")
-            for x in range(2):
-                self.module.warn(message, UserWarning)
-                if x == 0:
-                    self.assertEqual(w[-1].message, message)
-                    del w[:]
-                elif x == 1:
-                    self.assertEqual(len(w), 0)
-                else:
-                    raise ValueError("loop variant unhandled")
-
-    def test_module(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("module", category=UserWarning)
-            message = UserWarning("FilterTests.test_module")
-            self.module.warn(message, UserWarning)
-            self.assertEqual(w[-1].message, message)
-            del w[:]
-            self.module.warn(message, UserWarning)
-            self.assertEqual(len(w), 0)
-
-    def test_once(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("once", category=UserWarning)
-            message = UserWarning("FilterTests.test_once")
-            self.module.warn_explicit(message, UserWarning, "test_warnings.py",
-                                    42)
-            self.assertEqual(w[-1].message, message)
-            del w[:]
-            self.module.warn_explicit(message, UserWarning, "test_warnings.py",
-                                    13)
-            self.assertEqual(len(w), 0)
-            self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
-                                    42)
-            self.assertEqual(len(w), 0)
-
-    def test_inheritance(self):
-        with original_warnings.catch_warnings(module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("error", category=Warning)
-            self.assertRaises(UserWarning, self.module.warn,
-                                "FilterTests.test_inheritance", UserWarning)
-
-    def test_ordering(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("ignore", category=UserWarning)
-            self.module.filterwarnings("error", category=UserWarning,
-                                        append=True)
-            del w[:]
-            try:
-                self.module.warn("FilterTests.test_ordering", UserWarning)
-            except UserWarning:
-                self.fail("order handling for actions failed")
-            self.assertEqual(len(w), 0)
-
-    def test_filterwarnings(self):
-        # Test filterwarnings().
-        # Implicitly also tests resetwarnings().
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.filterwarnings("error", "", Warning, "", 0)
-            self.assertRaises(UserWarning, self.module.warn, 'convert to error')
-
-            self.module.resetwarnings()
-            text = 'handle normally'
-            self.module.warn(text)
-            self.assertEqual(str(w[-1].message), text)
-            self.assertTrue(w[-1].category is UserWarning)
-
-            self.module.filterwarnings("ignore", "", Warning, "", 0)
-            text = 'filtered out'
-            self.module.warn(text)
-            self.assertNotEqual(str(w[-1].message), text)
-
-            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)
-
-    def test_mutate_filter_list(self):
-        class X:
-            def match(self, a):
-                L[:] = []
-
-        L = [("default",X(),UserWarning,X(),0) for i in range(2)]
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.filters = L
-            self.module.warn_explicit(UserWarning("b"), None, "f.py", 42)
-            self.assertEqual(str(w[-1].message), "b")
-
-class CFilterTests(FilterTests, unittest.TestCase):
-    module = c_warnings
-
-class PyFilterTests(FilterTests, unittest.TestCase):
-    module = py_warnings
-
-
-class WarnTests(BaseTest):
-
-    """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):
-                text = 'multi %d' %i  # Different text on each call.
-                self.module.warn(text)
-                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,
-                    module=self.module) as w:
-                warning_tests.inner("spam1")
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "warning_tests.py")
-                warning_tests.outer("spam2")
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "warning_tests.py")
-
-    def test_stacklevel(self):
-        # Test stacklevel argument
-        # make sure all messages are different, so the warning won't be skipped
-        with warnings_state(self.module):
-            with original_warnings.catch_warnings(record=True,
-                    module=self.module) as w:
-                warning_tests.inner("spam3", stacklevel=1)
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "warning_tests.py")
-                warning_tests.outer("spam4", stacklevel=1)
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "warning_tests.py")
-
-                warning_tests.inner("spam5", stacklevel=2)
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "test_warnings.py")
-                warning_tests.outer("spam6", stacklevel=2)
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "warning_tests.py")
-                warning_tests.outer("spam6.5", stacklevel=3)
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "test_warnings.py")
-
-                warning_tests.inner("spam7", stacklevel=9999)
-                self.assertEqual(os.path.basename(w[-1].filename),
-                                    "sys")
-
-    def test_missing_filename_not_main(self):
-        # If __file__ is not specified and __main__ is not the module name,
-        # then __file__ should be set to the module name.
-        filename = warning_tests.__file__
-        try:
-            del warning_tests.__file__
-            with warnings_state(self.module):
-                with original_warnings.catch_warnings(record=True,
-                        module=self.module) as w:
-                    warning_tests.inner("spam8", stacklevel=1)
-                    self.assertEqual(w[-1].filename, warning_tests.__name__)
-        finally:
-            warning_tests.__file__ = filename
-
-    @unittest.skipUnless(hasattr(sys, 'argv'), 'test needs sys.argv')
-    def test_missing_filename_main_with_argv(self):
-        # If __file__ is not specified and the caller is __main__ and sys.argv
-        # exists, then use sys.argv[0] as the file.
-        filename = warning_tests.__file__
-        module_name = warning_tests.__name__
-        try:
-            del warning_tests.__file__
-            warning_tests.__name__ = '__main__'
-            with warnings_state(self.module):
-                with original_warnings.catch_warnings(record=True,
-                        module=self.module) as w:
-                    warning_tests.inner('spam9', stacklevel=1)
-                    self.assertEqual(w[-1].filename, sys.argv[0])
-        finally:
-            warning_tests.__file__ = filename
-            warning_tests.__name__ = module_name
-
-    def test_missing_filename_main_without_argv(self):
-        # If __file__ is not specified, the caller is __main__, and sys.argv
-        # is not set, then '__main__' is the file name.
-        filename = warning_tests.__file__
-        module_name = warning_tests.__name__
-        argv = sys.argv
-        try:
-            del warning_tests.__file__
-            warning_tests.__name__ = '__main__'
-            del sys.argv
-            with warnings_state(self.module):
-                with original_warnings.catch_warnings(record=True,
-                        module=self.module) as w:
-                    warning_tests.inner('spam10', stacklevel=1)
-                    self.assertEqual(w[-1].filename, '__main__')
-        finally:
-            warning_tests.__file__ = filename
-            warning_tests.__name__ = module_name
-            sys.argv = argv
-
-    def test_missing_filename_main_with_argv_empty_string(self):
-        # If __file__ is not specified, the caller is __main__, and sys.argv[0]
-        # is the empty string, then '__main__ is the file name.
-        # Tests issue 2743.
-        file_name = warning_tests.__file__
-        module_name = warning_tests.__name__
-        argv = sys.argv
-        try:
-            del warning_tests.__file__
-            warning_tests.__name__ = '__main__'
-            sys.argv = ['']
-            with warnings_state(self.module):
-                with original_warnings.catch_warnings(record=True,
-                        module=self.module) as w:
-                    warning_tests.inner('spam11', stacklevel=1)
-                    self.assertEqual(w[-1].filename, '__main__')
-        finally:
-            warning_tests.__file__ = file_name
-            warning_tests.__name__ = module_name
-            sys.argv = argv
-
-    def test_warn_explicit_non_ascii_filename(self):
-        with original_warnings.catch_warnings(record=True,
-                module=self.module) as w:
-            self.module.resetwarnings()
-            self.module.filterwarnings("always", category=UserWarning)
-            for filename in ("nonascii\xe9\u20ac", "surrogate\udc80"):
-                try:
-                    os.fsencode(filename)
-                except UnicodeEncodeError:
-                    continue
-                self.module.warn_explicit("text", UserWarning, filename, 1)
-                self.assertEqual(w[-1].filename, filename)
-
-    def test_warn_explicit_type_errors(self):
-        # warn_explicit() should error out gracefully if it is given objects
-        # of the wrong types.
-        # lineno is expected to be an integer.
-        self.assertRaises(TypeError, self.module.warn_explicit,
-                            None, UserWarning, None, None)
-        # Either 'message' needs to be an instance of Warning or 'category'
-        # needs to be a subclass.
-        self.assertRaises(TypeError, self.module.warn_explicit,
-                            None, None, None, 1)
-        # 'registry' must be a dict or None.
-        self.assertRaises((TypeError, AttributeError),
-                            self.module.warn_explicit,
-                            None, Warning, None, 1, registry=42)
-
-    def test_bad_str(self):
-        # issue 6415
-        # Warnings instance with a bad format string for __str__ should not
-        # trigger a bus error.
-        class BadStrWarning(Warning):
-            """Warning with a bad format string for __str__."""
-            def __str__(self):
-                return ("A bad formatted string %(err)" %
-                        {"err" : "there is no %(err)s"})
-
-        with self.assertRaises(ValueError):
-            self.module.warn(BadStrWarning())
-
-    def test_warning_classes(self):
-        class MyWarningClass(Warning):
-            pass
-
-        class NonWarningSubclass:
-            pass
-
-        # passing a non-subclass of Warning should raise a TypeError
-        with self.assertRaises(TypeError) as cm:
-            self.module.warn('bad warning category', '')
-        self.assertIn('category must be a Warning subclass, not ',
-                      str(cm.exception))
-
-        with self.assertRaises(TypeError) as cm:
-            self.module.warn('bad warning category', NonWarningSubclass)
-        self.assertIn('category must be a Warning subclass, not ',
-                      str(cm.exception))
-
-        # check that warning instances also raise a TypeError
-        with self.assertRaises(TypeError) as cm:
-            self.module.warn('bad warning category', MyWarningClass())
-        self.assertIn('category must be a Warning subclass, not ',
-                      str(cm.exception))
-
-        with original_warnings.catch_warnings(module=self.module):
-            self.module.resetwarnings()
-            self.module.filterwarnings('default')
-            with self.assertWarns(MyWarningClass) as cm:
-                self.module.warn('good warning category', MyWarningClass)
-            self.assertEqual('good warning category', str(cm.warning))
-
-            with self.assertWarns(UserWarning) as cm:
-                self.module.warn('good warning category', None)
-            self.assertEqual('good warning category', str(cm.warning))
-
-            with self.assertWarns(MyWarningClass) as cm:
-                self.module.warn('good warning category', MyWarningClass)
-            self.assertIsInstance(cm.warning, Warning)
-
-class CWarnTests(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(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(BaseTest):
-
-    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,
-                              self.module._setoption, 'bogus::Warning')
-            self.assertRaises(self.module._OptionError,
-                              self.module._setoption, 'ignore:2::4:-5')
-            self.module._setoption('error::Warning::0')
-            self.assertRaises(UserWarning, self.module.warn, 'convert to error')
-
-    def test_improper_option(self):
-        # Same as above, but check that the message is printed out when
-        # the interpreter is executed. This also checks that options are
-        # actually parsed at all.
-        rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
-        self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
-
-    def test_warnings_bootstrap(self):
-        # Check that the warnings module does get loaded when -W<some option>
-        # 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(WCmdLineTests, unittest.TestCase):
-    module = c_warnings
-
-class PyWCmdLineTests(WCmdLineTests, unittest.TestCase):
-    module = py_warnings
-
-
-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:
-            self.module.filterwarnings("error", "", Warning, "", 0)
-            self.assertRaises(UserWarning, self.module.warn,
-                                'convert to error')
-            del self.module.filters
-            self.assertRaises(UserWarning, self.module.warn,
-                                'convert to error')
-
-    def test_onceregistry(self):
-        # Replacing or removing the onceregistry should be okay.
-        global __warningregistry__
-        message = UserWarning('onceregistry test')
-        try:
-            original_registry = self.module.onceregistry
-            __warningregistry__ = {}
-            with original_warnings.catch_warnings(record=True,
-                    module=self.module) as w:
-                self.module.resetwarnings()
-                self.module.filterwarnings("once", category=UserWarning)
-                self.module.warn_explicit(message, UserWarning, "file", 42)
-                self.assertEqual(w[-1].message, message)
-                del w[:]
-                self.module.warn_explicit(message, UserWarning, "file", 42)
-                self.assertEqual(len(w), 0)
-                # Test the resetting of onceregistry.
-                self.module.onceregistry = {}
-                __warningregistry__ = {}
-                self.module.warn('onceregistry test')
-                self.assertEqual(w[-1].message.args, message.args)
-                # Removal of onceregistry is okay.
-                del w[:]
-                del self.module.onceregistry
-                __warningregistry__ = {}
-                self.module.warn_explicit(message, UserWarning, "file", 42)
-                self.assertEqual(len(w), 0)
-        finally:
-            self.module.onceregistry = original_registry
-
-    def test_default_action(self):
-        # Replacing or removing defaultaction should be okay.
-        message = UserWarning("defaultaction test")
-        original = self.module.defaultaction
-        try:
-            with original_warnings.catch_warnings(record=True,
-                    module=self.module) as w:
-                self.module.resetwarnings()
-                registry = {}
-                self.module.warn_explicit(message, UserWarning, "<test>", 42,
-                                            registry=registry)
-                self.assertEqual(w[-1].message, message)
-                self.assertEqual(len(w), 1)
-                # One actual registry key plus the "version" key
-                self.assertEqual(len(registry), 2)
-                self.assertIn("version", registry)
-                del w[:]
-                # Test removal.
-                del self.module.defaultaction
-                __warningregistry__ = {}
-                registry = {}
-                self.module.warn_explicit(message, UserWarning, "<test>", 43,
-                                            registry=registry)
-                self.assertEqual(w[-1].message, message)
-                self.assertEqual(len(w), 1)
-                self.assertEqual(len(registry), 2)
-                del w[:]
-                # Test setting.
-                self.module.defaultaction = "ignore"
-                __warningregistry__ = {}
-                registry = {}
-                self.module.warn_explicit(message, UserWarning, "<test>", 44,
-                                            registry=registry)
-                self.assertEqual(len(w), 0)
-        finally:
-            self.module.defaultaction = original
-
-    def test_showwarning_missing(self):
-        # Test that showwarning() missing is okay.
-        text = 'del showwarning test'
-        with original_warnings.catch_warnings(module=self.module):
-            self.module.filterwarnings("always", category=UserWarning)
-            del self.module.showwarning
-            with support.captured_output('stderr') as stream:
-                self.module.warn(text)
-                result = stream.getvalue()
-        self.assertIn(text, result)
-
-    def test_showwarning_not_callable(self):
-        with original_warnings.catch_warnings(module=self.module):
-            self.module.filterwarnings("always", category=UserWarning)
-            self.module.showwarning = print
-            with support.captured_output('stdout'):
-                self.module.warn('Warning!')
-            self.module.showwarning = 23
-            self.assertRaises(TypeError, self.module.warn, "Warning!")
-
-    def test_show_warning_output(self):
-        # With showarning() missing, make sure that output is okay.
-        text = 'test show_warning'
-        with original_warnings.catch_warnings(module=self.module):
-            self.module.filterwarnings("always", category=UserWarning)
-            del self.module.showwarning
-            with support.captured_output('stderr') as stream:
-                warning_tests.inner(text)
-                result = stream.getvalue()
-        self.assertEqual(result.count('\n'), 2,
-                             "Too many newlines in %r" % result)
-        first_line, second_line = result.split('\n', 1)
-        expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
-        first_line_parts = first_line.rsplit(':', 3)
-        path, line, warning_class, message = first_line_parts
-        line = int(line)
-        self.assertEqual(expected_file, path)
-        self.assertEqual(warning_class, ' ' + UserWarning.__name__)
-        self.assertEqual(message, ' ' + text)
-        expected_line = '  ' + linecache.getline(path, line).strip() + '\n'
-        assert expected_line
-        self.assertEqual(second_line, expected_line)
-
-    def test_filename_none(self):
-        # issue #12467: race condition if a warning is emitted at shutdown
-        globals_dict = globals()
-        oldfile = globals_dict['__file__']
-        try:
-            catch = original_warnings.catch_warnings(record=True,
-                                                     module=self.module)
-            with catch as w:
-                self.module.filterwarnings("always", category=UserWarning)
-                globals_dict['__file__'] = None
-                original_warnings.warn('test', UserWarning)
-                self.assertTrue(len(w))
-        finally:
-            globals_dict['__file__'] = oldfile
-
-    def test_stderr_none(self):
-        rc, stdout, stderr = assert_python_ok("-c",
-            "import sys; sys.stderr = None; "
-            "import warnings; warnings.simplefilter('always'); "
-            "warnings.warn('Warning!')")
-        self.assertEqual(stdout, b'')
-        self.assertNotIn(b'Warning!', stderr)
-        self.assertNotIn(b'Error', stderr)
-
-
-class WarningsDisplayTests(BaseTest):
-
-    """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'
-        line_num = 3
-        file_line = linecache.getline(file_name, line_num).strip()
-        format = "%s:%s: %s: %s\n  %s\n"
-        expect = format % (file_name, line_num, category.__name__, message,
-                            file_line)
-        self.assertEqual(expect, self.module.formatwarning(message,
-                                                category, file_name, line_num))
-        # Test the 'line' argument.
-        file_line += " for the win!"
-        expect = format % (file_name, line_num, category.__name__, message,
-                            file_line)
-        self.assertEqual(expect, self.module.formatwarning(message,
-                                    category, file_name, line_num, file_line))
-
-    def test_showwarning(self):
-        file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
-        line_num = 3
-        expected_file_line = linecache.getline(file_name, line_num).strip()
-        message = 'msg'
-        category = Warning
-        file_object = StringIO()
-        expect = self.module.formatwarning(message, category, file_name,
-                                            line_num)
-        self.module.showwarning(message, category, file_name, line_num,
-                                file_object)
-        self.assertEqual(file_object.getvalue(), expect)
-        # Test 'line' argument.
-        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(WarningsDisplayTests, unittest.TestCase):
-    module = c_warnings
-
-class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
-    module = py_warnings
-
-
-class CatchWarningTests(BaseTest):
-
-    """Test catch_warnings()."""
-
-    def test_catch_warnings_restore(self):
-        wmod = self.module
-        orig_filters = wmod.filters
-        orig_showwarning = wmod.showwarning
-        # Ensure both showwarning and filters are restored when recording
-        with wmod.catch_warnings(module=wmod, record=True):
-            wmod.filters = wmod.showwarning = object()
-        self.assertTrue(wmod.filters is orig_filters)
-        self.assertTrue(wmod.showwarning is orig_showwarning)
-        # Same test, but with recording disabled
-        with wmod.catch_warnings(module=wmod, record=False):
-            wmod.filters = wmod.showwarning = object()
-        self.assertTrue(wmod.filters is orig_filters)
-        self.assertTrue(wmod.showwarning is orig_showwarning)
-
-    def test_catch_warnings_recording(self):
-        wmod = self.module
-        # Ensure warnings are recorded when requested
-        with wmod.catch_warnings(module=wmod, record=True) as w:
-            self.assertEqual(w, [])
-            self.assertTrue(type(w) is list)
-            wmod.simplefilter("always")
-            wmod.warn("foo")
-            self.assertEqual(str(w[-1].message), "foo")
-            wmod.warn("bar")
-            self.assertEqual(str(w[-1].message), "bar")
-            self.assertEqual(str(w[0].message), "foo")
-            self.assertEqual(str(w[1].message), "bar")
-            del w[:]
-            self.assertEqual(w, [])
-        # Ensure warnings are not recorded when not requested
-        orig_showwarning = wmod.showwarning
-        with wmod.catch_warnings(module=wmod, record=False) as w:
-            self.assertTrue(w is None)
-            self.assertTrue(wmod.showwarning is orig_showwarning)
-
-    def test_catch_warnings_reentry_guard(self):
-        wmod = self.module
-        # Ensure catch_warnings is protected against incorrect usage
-        x = wmod.catch_warnings(module=wmod, record=True)
-        self.assertRaises(RuntimeError, x.__exit__)
-        with x:
-            self.assertRaises(RuntimeError, x.__enter__)
-        # Same test, but with recording disabled
-        x = wmod.catch_warnings(module=wmod, record=False)
-        self.assertRaises(RuntimeError, x.__exit__)
-        with x:
-            self.assertRaises(RuntimeError, x.__enter__)
-
-    def test_catch_warnings_defaults(self):
-        wmod = self.module
-        orig_filters = wmod.filters
-        orig_showwarning = wmod.showwarning
-        # Ensure default behaviour is not to record warnings
-        with wmod.catch_warnings(module=wmod) as w:
-            self.assertTrue(w is None)
-            self.assertTrue(wmod.showwarning is orig_showwarning)
-            self.assertTrue(wmod.filters is not orig_filters)
-        self.assertTrue(wmod.filters is orig_filters)
-        if wmod is sys.modules['warnings']:
-            # Ensure the default module is this one
-            with wmod.catch_warnings() as w:
-                self.assertTrue(w is None)
-                self.assertTrue(wmod.showwarning is orig_showwarning)
-                self.assertTrue(wmod.filters is not orig_filters)
-            self.assertTrue(wmod.filters is orig_filters)
-
-    def test_check_warnings(self):
-        # Explicit tests for the test.support convenience wrapper
-        wmod = self.module
-        if wmod is not sys.modules['warnings']:
-            self.skipTest('module to test is not loaded warnings module')
-        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")
-
-class CCatchWarningTests(CatchWarningTests, unittest.TestCase):
-    module = c_warnings
-
-class PyCatchWarningTests(CatchWarningTests, unittest.TestCase):
-    module = py_warnings
-
-
-class EnvironmentVariableTests(BaseTest):
-
-    def test_single_warning(self):
-        rc, stdout, stderr = assert_python_ok("-c",
-            "import sys; sys.stdout.write(str(sys.warnoptions))",
-            PYTHONWARNINGS="ignore::DeprecationWarning")
-        self.assertEqual(stdout, b"['ignore::DeprecationWarning']")
-
-    def test_comma_separated_warnings(self):
-        rc, stdout, stderr = assert_python_ok("-c",
-            "import sys; sys.stdout.write(str(sys.warnoptions))",
-            PYTHONWARNINGS="ignore::DeprecationWarning,ignore::UnicodeWarning")
-        self.assertEqual(stdout,
-            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
-
-    def test_envvar_and_command_line(self):
-        rc, stdout, stderr = assert_python_ok("-Wignore::UnicodeWarning", "-c",
-            "import sys; sys.stdout.write(str(sys.warnoptions))",
-            PYTHONWARNINGS="ignore::DeprecationWarning")
-        self.assertEqual(stdout,
-            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
-
-    def test_conflicting_envvar_and_command_line(self):
-        rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
-            "import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
-            "warnings.warn('Message', DeprecationWarning)",
-            PYTHONWARNINGS="default::DeprecationWarning")
-        self.assertEqual(stdout,
-            b"['default::DeprecationWarning', 'error::DeprecationWarning']")
-        self.assertEqual(stderr.splitlines(),
-            [b"Traceback (most recent call last):",
-             b"  File \"<string>\", line 1, in <module>",
-             b"DeprecationWarning: Message"])
-
-    @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
-                         'requires non-ascii filesystemencoding')
-    def test_nonascii(self):
-        rc, stdout, stderr = assert_python_ok("-c",
-            "import sys; sys.stdout.write(str(sys.warnoptions))",
-            PYTHONIOENCODING="utf-8",
-            PYTHONWARNINGS="ignore:DeprecaciónWarning")
-        self.assertEqual(stdout,
-            "['ignore:DeprecaciónWarning']".encode('utf-8'))
-
-class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
-    module = c_warnings
-
-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
-        with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
-            # encodings loaded by initfsencoding()
-            assert_python_ok('-c', 'pass', PYTHONPATH=cwd)
-
-            # Use -W to load warnings module at startup
-            assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd)
-
-class FinalizationTest(unittest.TestCase):
-    def test_finalization(self):
-        # Issue #19421: warnings.warn() should not crash
-        # during Python finalization
-        code = """
-import warnings
-warn = warnings.warn
-
-class A:
-    def __del__(self):
-        warn("test")
-
-a=A()
-        """
-        rc, out, err = assert_python_ok("-c", code)
-        # note: "__main__" filename is not correct, it should be the name
-        # of the script
-        self.assertEqual(err, b'__main__:7: UserWarning: test')
-
-
-def setUpModule():
-    py_warnings.onceregistry.clear()
-    c_warnings.onceregistry.clear()
-
-tearDownModule = setUpModule
-
-if __name__ == "__main__":
-    unittest.main()
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_warnings/__init__.py
@@ -0,0 +1,955 @@
+from contextlib import contextmanager
+import linecache
+import os
+from io import StringIO
+import sys
+import unittest
+from test import support
+from test.support.script_helper import assert_python_ok, assert_python_failure
+
+from test.test_warnings.data import stacklevel as warning_tests
+
+import warnings as original_warnings
+
+py_warnings = support.import_fresh_module('warnings', blocked=['_warnings'])
+c_warnings = support.import_fresh_module('warnings', fresh=['_warnings'])
+
+ at contextmanager
+def warnings_state(module):
+    """Use a specific warnings implementation in warning_tests."""
+    global __warningregistry__
+    for to_clear in (sys, warning_tests):
+        try:
+            to_clear.__warningregistry__.clear()
+        except AttributeError:
+            pass
+    try:
+        __warningregistry__.clear()
+    except NameError:
+        pass
+    original_warnings = warning_tests.warnings
+    original_filters = module.filters
+    try:
+        module.filters = original_filters[:]
+        module.simplefilter("once")
+        warning_tests.warnings = module
+        yield
+    finally:
+        warning_tests.warnings = original_warnings
+        module.filters = original_filters
+
+
+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__']
+        if hasattr(warning_tests, '__warningregistry__'):
+            del warning_tests.__warningregistry__
+        if hasattr(sys, '__warningregistry__'):
+            del sys.__warningregistry__
+        # The 'warnings' module must be explicitly set so that the proper
+        # interaction between _warnings and 'warnings' can be controlled.
+        sys.modules['warnings'] = self.module
+        super(BaseTest, self).setUp()
+
+    def tearDown(self):
+        sys.modules['warnings'] = original_warnings
+        super(BaseTest, self).tearDown()
+
+class PublicAPITests(BaseTest):
+
+    """Ensures that the correct values are exposed in the
+    public API.
+    """
+
+    def test_module_all_attribute(self):
+        self.assertTrue(hasattr(self.module, '__all__'))
+        target_api = ["warn", "warn_explicit", "showwarning",
+                      "formatwarning", "filterwarnings", "simplefilter",
+                      "resetwarnings", "catch_warnings"]
+        self.assertSetEqual(set(self.module.__all__),
+                            set(target_api))
+
+class CPublicAPITests(PublicAPITests, unittest.TestCase):
+    module = c_warnings
+
+class PyPublicAPITests(PublicAPITests, unittest.TestCase):
+    module = py_warnings
+
+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,
+                                "FilterTests.test_error")
+
+    def test_error_after_default(self):
+        with original_warnings.catch_warnings(module=self.module) as w:
+            self.module.resetwarnings()
+            message = "FilterTests.test_ignore_after_default"
+            def f():
+                self.module.warn(message, UserWarning)
+            f()
+            self.module.filterwarnings("error", category=UserWarning)
+            self.assertRaises(UserWarning, f)
+
+    def test_ignore(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("ignore", category=UserWarning)
+            self.module.warn("FilterTests.test_ignore", UserWarning)
+            self.assertEqual(len(w), 0)
+
+    def test_ignore_after_default(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            message = "FilterTests.test_ignore_after_default"
+            def f():
+                self.module.warn(message, UserWarning)
+            f()
+            self.module.filterwarnings("ignore", category=UserWarning)
+            f()
+            f()
+            self.assertEqual(len(w), 1)
+
+    def test_always(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("always", category=UserWarning)
+            message = "FilterTests.test_always"
+            self.module.warn(message, UserWarning)
+            self.assertTrue(message, w[-1].message)
+            self.module.warn(message, UserWarning)
+            self.assertTrue(w[-1].message, message)
+
+    def test_always_after_default(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            message = "FilterTests.test_always_after_ignore"
+            def f():
+                self.module.warn(message, UserWarning)
+            f()
+            self.assertEqual(len(w), 1)
+            self.assertEqual(w[-1].message.args[0], message)
+            f()
+            self.assertEqual(len(w), 1)
+            self.module.filterwarnings("always", category=UserWarning)
+            f()
+            self.assertEqual(len(w), 2)
+            self.assertEqual(w[-1].message.args[0], message)
+            f()
+            self.assertEqual(len(w), 3)
+            self.assertEqual(w[-1].message.args[0], message)
+
+    def test_default(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("default", category=UserWarning)
+            message = UserWarning("FilterTests.test_default")
+            for x in range(2):
+                self.module.warn(message, UserWarning)
+                if x == 0:
+                    self.assertEqual(w[-1].message, message)
+                    del w[:]
+                elif x == 1:
+                    self.assertEqual(len(w), 0)
+                else:
+                    raise ValueError("loop variant unhandled")
+
+    def test_module(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("module", category=UserWarning)
+            message = UserWarning("FilterTests.test_module")
+            self.module.warn(message, UserWarning)
+            self.assertEqual(w[-1].message, message)
+            del w[:]
+            self.module.warn(message, UserWarning)
+            self.assertEqual(len(w), 0)
+
+    def test_once(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("once", category=UserWarning)
+            message = UserWarning("FilterTests.test_once")
+            self.module.warn_explicit(message, UserWarning, "__init__.py",
+                                    42)
+            self.assertEqual(w[-1].message, message)
+            del w[:]
+            self.module.warn_explicit(message, UserWarning, "__init__.py",
+                                    13)
+            self.assertEqual(len(w), 0)
+            self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
+                                    42)
+            self.assertEqual(len(w), 0)
+
+    def test_inheritance(self):
+        with original_warnings.catch_warnings(module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("error", category=Warning)
+            self.assertRaises(UserWarning, self.module.warn,
+                                "FilterTests.test_inheritance", UserWarning)
+
+    def test_ordering(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("ignore", category=UserWarning)
+            self.module.filterwarnings("error", category=UserWarning,
+                                        append=True)
+            del w[:]
+            try:
+                self.module.warn("FilterTests.test_ordering", UserWarning)
+            except UserWarning:
+                self.fail("order handling for actions failed")
+            self.assertEqual(len(w), 0)
+
+    def test_filterwarnings(self):
+        # Test filterwarnings().
+        # Implicitly also tests resetwarnings().
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.filterwarnings("error", "", Warning, "", 0)
+            self.assertRaises(UserWarning, self.module.warn, 'convert to error')
+
+            self.module.resetwarnings()
+            text = 'handle normally'
+            self.module.warn(text)
+            self.assertEqual(str(w[-1].message), text)
+            self.assertTrue(w[-1].category is UserWarning)
+
+            self.module.filterwarnings("ignore", "", Warning, "", 0)
+            text = 'filtered out'
+            self.module.warn(text)
+            self.assertNotEqual(str(w[-1].message), text)
+
+            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)
+
+    def test_mutate_filter_list(self):
+        class X:
+            def match(self, a):
+                L[:] = []
+
+        L = [("default",X(),UserWarning,X(),0) for i in range(2)]
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.filters = L
+            self.module.warn_explicit(UserWarning("b"), None, "f.py", 42)
+            self.assertEqual(str(w[-1].message), "b")
+
+class CFilterTests(FilterTests, unittest.TestCase):
+    module = c_warnings
+
+class PyFilterTests(FilterTests, unittest.TestCase):
+    module = py_warnings
+
+
+class WarnTests(BaseTest):
+
+    """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):
+                text = 'multi %d' %i  # Different text on each call.
+                self.module.warn(text)
+                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,
+                    module=self.module) as w:
+                warning_tests.inner("spam1")
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "stacklevel.py")
+                warning_tests.outer("spam2")
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "stacklevel.py")
+
+    def test_stacklevel(self):
+        # Test stacklevel argument
+        # make sure all messages are different, so the warning won't be skipped
+        with warnings_state(self.module):
+            with original_warnings.catch_warnings(record=True,
+                    module=self.module) as w:
+                warning_tests.inner("spam3", stacklevel=1)
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "stacklevel.py")
+                warning_tests.outer("spam4", stacklevel=1)
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "stacklevel.py")
+
+                warning_tests.inner("spam5", stacklevel=2)
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "__init__.py")
+                warning_tests.outer("spam6", stacklevel=2)
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "stacklevel.py")
+                warning_tests.outer("spam6.5", stacklevel=3)
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "__init__.py")
+
+                warning_tests.inner("spam7", stacklevel=9999)
+                self.assertEqual(os.path.basename(w[-1].filename),
+                                    "sys")
+
+    def test_stacklevel_import(self):
+        # Issue #24305: With stacklevel=2, module-level warnings should work.
+        support.unload('test.test_warnings.data.import_warning')
+        with warnings_state(self.module):
+            with original_warnings.catch_warnings(record=True,
+                    module=self.module) as w:
+                self.module.simplefilter('always')
+                import test.test_warnings.data.import_warning
+                self.assertEqual(len(w), 1)
+                self.assertEqual(w[0].filename, __file__)
+
+    def test_missing_filename_not_main(self):
+        # If __file__ is not specified and __main__ is not the module name,
+        # then __file__ should be set to the module name.
+        filename = warning_tests.__file__
+        try:
+            del warning_tests.__file__
+            with warnings_state(self.module):
+                with original_warnings.catch_warnings(record=True,
+                        module=self.module) as w:
+                    warning_tests.inner("spam8", stacklevel=1)
+                    self.assertEqual(w[-1].filename, warning_tests.__name__)
+        finally:
+            warning_tests.__file__ = filename
+
+    @unittest.skipUnless(hasattr(sys, 'argv'), 'test needs sys.argv')
+    def test_missing_filename_main_with_argv(self):
+        # If __file__ is not specified and the caller is __main__ and sys.argv
+        # exists, then use sys.argv[0] as the file.
+        filename = warning_tests.__file__
+        module_name = warning_tests.__name__
+        try:
+            del warning_tests.__file__
+            warning_tests.__name__ = '__main__'
+            with warnings_state(self.module):
+                with original_warnings.catch_warnings(record=True,
+                        module=self.module) as w:
+                    warning_tests.inner('spam9', stacklevel=1)
+                    self.assertEqual(w[-1].filename, sys.argv[0])
+        finally:
+            warning_tests.__file__ = filename
+            warning_tests.__name__ = module_name
+
+    def test_missing_filename_main_without_argv(self):
+        # If __file__ is not specified, the caller is __main__, and sys.argv
+        # is not set, then '__main__' is the file name.
+        filename = warning_tests.__file__
+        module_name = warning_tests.__name__
+        argv = sys.argv
+        try:
+            del warning_tests.__file__
+            warning_tests.__name__ = '__main__'
+            del sys.argv
+            with warnings_state(self.module):
+                with original_warnings.catch_warnings(record=True,
+                        module=self.module) as w:
+                    warning_tests.inner('spam10', stacklevel=1)
+                    self.assertEqual(w[-1].filename, '__main__')
+        finally:
+            warning_tests.__file__ = filename
+            warning_tests.__name__ = module_name
+            sys.argv = argv
+
+    def test_missing_filename_main_with_argv_empty_string(self):
+        # If __file__ is not specified, the caller is __main__, and sys.argv[0]
+        # is the empty string, then '__main__ is the file name.
+        # Tests issue 2743.
+        file_name = warning_tests.__file__
+        module_name = warning_tests.__name__
+        argv = sys.argv
+        try:
+            del warning_tests.__file__
+            warning_tests.__name__ = '__main__'
+            sys.argv = ['']
+            with warnings_state(self.module):
+                with original_warnings.catch_warnings(record=True,
+                        module=self.module) as w:
+                    warning_tests.inner('spam11', stacklevel=1)
+                    self.assertEqual(w[-1].filename, '__main__')
+        finally:
+            warning_tests.__file__ = file_name
+            warning_tests.__name__ = module_name
+            sys.argv = argv
+
+    def test_warn_explicit_non_ascii_filename(self):
+        with original_warnings.catch_warnings(record=True,
+                module=self.module) as w:
+            self.module.resetwarnings()
+            self.module.filterwarnings("always", category=UserWarning)
+            for filename in ("nonascii\xe9\u20ac", "surrogate\udc80"):
+                try:
+                    os.fsencode(filename)
+                except UnicodeEncodeError:
+                    continue
+                self.module.warn_explicit("text", UserWarning, filename, 1)
+                self.assertEqual(w[-1].filename, filename)
+
+    def test_warn_explicit_type_errors(self):
+        # warn_explicit() should error out gracefully if it is given objects
+        # of the wrong types.
+        # lineno is expected to be an integer.
+        self.assertRaises(TypeError, self.module.warn_explicit,
+                            None, UserWarning, None, None)
+        # Either 'message' needs to be an instance of Warning or 'category'
+        # needs to be a subclass.
+        self.assertRaises(TypeError, self.module.warn_explicit,
+                            None, None, None, 1)
+        # 'registry' must be a dict or None.
+        self.assertRaises((TypeError, AttributeError),
+                            self.module.warn_explicit,
+                            None, Warning, None, 1, registry=42)
+
+    def test_bad_str(self):
+        # issue 6415
+        # Warnings instance with a bad format string for __str__ should not
+        # trigger a bus error.
+        class BadStrWarning(Warning):
+            """Warning with a bad format string for __str__."""
+            def __str__(self):
+                return ("A bad formatted string %(err)" %
+                        {"err" : "there is no %(err)s"})
+
+        with self.assertRaises(ValueError):
+            self.module.warn(BadStrWarning())
+
+    def test_warning_classes(self):
+        class MyWarningClass(Warning):
+            pass
+
+        class NonWarningSubclass:
+            pass
+
+        # passing a non-subclass of Warning should raise a TypeError
+        with self.assertRaises(TypeError) as cm:
+            self.module.warn('bad warning category', '')
+        self.assertIn('category must be a Warning subclass, not ',
+                      str(cm.exception))
+
+        with self.assertRaises(TypeError) as cm:
+            self.module.warn('bad warning category', NonWarningSubclass)
+        self.assertIn('category must be a Warning subclass, not ',
+                      str(cm.exception))
+
+        # check that warning instances also raise a TypeError
+        with self.assertRaises(TypeError) as cm:
+            self.module.warn('bad warning category', MyWarningClass())
+        self.assertIn('category must be a Warning subclass, not ',
+                      str(cm.exception))
+
+        with original_warnings.catch_warnings(module=self.module):
+            self.module.resetwarnings()
+            self.module.filterwarnings('default')
+            with self.assertWarns(MyWarningClass) as cm:
+                self.module.warn('good warning category', MyWarningClass)
+            self.assertEqual('good warning category', str(cm.warning))
+
+            with self.assertWarns(UserWarning) as cm:
+                self.module.warn('good warning category', None)
+            self.assertEqual('good warning category', str(cm.warning))
+
+            with self.assertWarns(MyWarningClass) as cm:
+                self.module.warn('good warning category', MyWarningClass)
+            self.assertIsInstance(cm.warning, Warning)
+
+class CWarnTests(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(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(BaseTest):
+
+    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,
+                              self.module._setoption, 'bogus::Warning')
+            self.assertRaises(self.module._OptionError,
+                              self.module._setoption, 'ignore:2::4:-5')
+            self.module._setoption('error::Warning::0')
+            self.assertRaises(UserWarning, self.module.warn, 'convert to error')
+
+    def test_improper_option(self):
+        # Same as above, but check that the message is printed out when
+        # the interpreter is executed. This also checks that options are
+        # actually parsed at all.
+        rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
+        self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
+
+    def test_warnings_bootstrap(self):
+        # Check that the warnings module does get loaded when -W<some option>
+        # 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(WCmdLineTests, unittest.TestCase):
+    module = c_warnings
+
+class PyWCmdLineTests(WCmdLineTests, unittest.TestCase):
+    module = py_warnings
+
+
+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:
+            self.module.filterwarnings("error", "", Warning, "", 0)
+            self.assertRaises(UserWarning, self.module.warn,
+                                'convert to error')
+            del self.module.filters
+            self.assertRaises(UserWarning, self.module.warn,
+                                'convert to error')
+
+    def test_onceregistry(self):
+        # Replacing or removing the onceregistry should be okay.
+        global __warningregistry__
+        message = UserWarning('onceregistry test')
+        try:
+            original_registry = self.module.onceregistry
+            __warningregistry__ = {}
+            with original_warnings.catch_warnings(record=True,
+                    module=self.module) as w:
+                self.module.resetwarnings()
+                self.module.filterwarnings("once", category=UserWarning)
+                self.module.warn_explicit(message, UserWarning, "file", 42)
+                self.assertEqual(w[-1].message, message)
+                del w[:]
+                self.module.warn_explicit(message, UserWarning, "file", 42)
+                self.assertEqual(len(w), 0)
+                # Test the resetting of onceregistry.
+                self.module.onceregistry = {}
+                __warningregistry__ = {}
+                self.module.warn('onceregistry test')
+                self.assertEqual(w[-1].message.args, message.args)
+                # Removal of onceregistry is okay.
+                del w[:]
+                del self.module.onceregistry
+                __warningregistry__ = {}
+                self.module.warn_explicit(message, UserWarning, "file", 42)
+                self.assertEqual(len(w), 0)
+        finally:
+            self.module.onceregistry = original_registry
+
+    def test_default_action(self):
+        # Replacing or removing defaultaction should be okay.
+        message = UserWarning("defaultaction test")
+        original = self.module.defaultaction
+        try:
+            with original_warnings.catch_warnings(record=True,
+                    module=self.module) as w:
+                self.module.resetwarnings()
+                registry = {}
+                self.module.warn_explicit(message, UserWarning, "<test>", 42,
+                                            registry=registry)
+                self.assertEqual(w[-1].message, message)
+                self.assertEqual(len(w), 1)
+                # One actual registry key plus the "version" key
+                self.assertEqual(len(registry), 2)
+                self.assertIn("version", registry)
+                del w[:]
+                # Test removal.
+                del self.module.defaultaction
+                __warningregistry__ = {}
+                registry = {}
+                self.module.warn_explicit(message, UserWarning, "<test>", 43,
+                                            registry=registry)
+                self.assertEqual(w[-1].message, message)
+                self.assertEqual(len(w), 1)
+                self.assertEqual(len(registry), 2)
+                del w[:]
+                # Test setting.
+                self.module.defaultaction = "ignore"
+                __warningregistry__ = {}
+                registry = {}
+                self.module.warn_explicit(message, UserWarning, "<test>", 44,
+                                            registry=registry)
+                self.assertEqual(len(w), 0)
+        finally:
+            self.module.defaultaction = original
+
+    def test_showwarning_missing(self):
+        # Test that showwarning() missing is okay.
+        text = 'del showwarning test'
+        with original_warnings.catch_warnings(module=self.module):
+            self.module.filterwarnings("always", category=UserWarning)
+            del self.module.showwarning
+            with support.captured_output('stderr') as stream:
+                self.module.warn(text)
+                result = stream.getvalue()
+        self.assertIn(text, result)
+
+    def test_showwarning_not_callable(self):
+        with original_warnings.catch_warnings(module=self.module):
+            self.module.filterwarnings("always", category=UserWarning)
+            self.module.showwarning = print
+            with support.captured_output('stdout'):
+                self.module.warn('Warning!')
+            self.module.showwarning = 23
+            self.assertRaises(TypeError, self.module.warn, "Warning!")
+
+    def test_show_warning_output(self):
+        # With showarning() missing, make sure that output is okay.
+        text = 'test show_warning'
+        with original_warnings.catch_warnings(module=self.module):
+            self.module.filterwarnings("always", category=UserWarning)
+            del self.module.showwarning
+            with support.captured_output('stderr') as stream:
+                warning_tests.inner(text)
+                result = stream.getvalue()
+        self.assertEqual(result.count('\n'), 2,
+                             "Too many newlines in %r" % result)
+        first_line, second_line = result.split('\n', 1)
+        expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
+        first_line_parts = first_line.rsplit(':', 3)
+        path, line, warning_class, message = first_line_parts
+        line = int(line)
+        self.assertEqual(expected_file, path)
+        self.assertEqual(warning_class, ' ' + UserWarning.__name__)
+        self.assertEqual(message, ' ' + text)
+        expected_line = '  ' + linecache.getline(path, line).strip() + '\n'
+        assert expected_line
+        self.assertEqual(second_line, expected_line)
+
+    def test_filename_none(self):
+        # issue #12467: race condition if a warning is emitted at shutdown
+        globals_dict = globals()
+        oldfile = globals_dict['__file__']
+        try:
+            catch = original_warnings.catch_warnings(record=True,
+                                                     module=self.module)
+            with catch as w:
+                self.module.filterwarnings("always", category=UserWarning)
+                globals_dict['__file__'] = None
+                original_warnings.warn('test', UserWarning)
+                self.assertTrue(len(w))
+        finally:
+            globals_dict['__file__'] = oldfile
+
+    def test_stderr_none(self):
+        rc, stdout, stderr = assert_python_ok("-c",
+            "import sys; sys.stderr = None; "
+            "import warnings; warnings.simplefilter('always'); "
+            "warnings.warn('Warning!')")
+        self.assertEqual(stdout, b'')
+        self.assertNotIn(b'Warning!', stderr)
+        self.assertNotIn(b'Error', stderr)
+
+
+class WarningsDisplayTests(BaseTest):
+
+    """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'
+        line_num = 3
+        file_line = linecache.getline(file_name, line_num).strip()
+        format = "%s:%s: %s: %s\n  %s\n"
+        expect = format % (file_name, line_num, category.__name__, message,
+                            file_line)
+        self.assertEqual(expect, self.module.formatwarning(message,
+                                                category, file_name, line_num))
+        # Test the 'line' argument.
+        file_line += " for the win!"
+        expect = format % (file_name, line_num, category.__name__, message,
+                            file_line)
+        self.assertEqual(expect, self.module.formatwarning(message,
+                                    category, file_name, line_num, file_line))
+
+    def test_showwarning(self):
+        file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
+        line_num = 3
+        expected_file_line = linecache.getline(file_name, line_num).strip()
+        message = 'msg'
+        category = Warning
+        file_object = StringIO()
+        expect = self.module.formatwarning(message, category, file_name,
+                                            line_num)
+        self.module.showwarning(message, category, file_name, line_num,
+                                file_object)
+        self.assertEqual(file_object.getvalue(), expect)
+        # Test 'line' argument.
+        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(WarningsDisplayTests, unittest.TestCase):
+    module = c_warnings
+
+class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
+    module = py_warnings
+
+
+class CatchWarningTests(BaseTest):
+
+    """Test catch_warnings()."""
+
+    def test_catch_warnings_restore(self):
+        wmod = self.module
+        orig_filters = wmod.filters
+        orig_showwarning = wmod.showwarning
+        # Ensure both showwarning and filters are restored when recording
+        with wmod.catch_warnings(module=wmod, record=True):
+            wmod.filters = wmod.showwarning = object()
+        self.assertTrue(wmod.filters is orig_filters)
+        self.assertTrue(wmod.showwarning is orig_showwarning)
+        # Same test, but with recording disabled
+        with wmod.catch_warnings(module=wmod, record=False):
+            wmod.filters = wmod.showwarning = object()
+        self.assertTrue(wmod.filters is orig_filters)
+        self.assertTrue(wmod.showwarning is orig_showwarning)
+
+    def test_catch_warnings_recording(self):
+        wmod = self.module
+        # Ensure warnings are recorded when requested
+        with wmod.catch_warnings(module=wmod, record=True) as w:
+            self.assertEqual(w, [])
+            self.assertTrue(type(w) is list)
+            wmod.simplefilter("always")
+            wmod.warn("foo")
+            self.assertEqual(str(w[-1].message), "foo")
+            wmod.warn("bar")
+            self.assertEqual(str(w[-1].message), "bar")
+            self.assertEqual(str(w[0].message), "foo")
+            self.assertEqual(str(w[1].message), "bar")
+            del w[:]
+            self.assertEqual(w, [])
+        # Ensure warnings are not recorded when not requested
+        orig_showwarning = wmod.showwarning
+        with wmod.catch_warnings(module=wmod, record=False) as w:
+            self.assertTrue(w is None)
+            self.assertTrue(wmod.showwarning is orig_showwarning)
+
+    def test_catch_warnings_reentry_guard(self):
+        wmod = self.module
+        # Ensure catch_warnings is protected against incorrect usage
+        x = wmod.catch_warnings(module=wmod, record=True)
+        self.assertRaises(RuntimeError, x.__exit__)
+        with x:
+            self.assertRaises(RuntimeError, x.__enter__)
+        # Same test, but with recording disabled
+        x = wmod.catch_warnings(module=wmod, record=False)
+        self.assertRaises(RuntimeError, x.__exit__)
+        with x:
+            self.assertRaises(RuntimeError, x.__enter__)
+
+    def test_catch_warnings_defaults(self):
+        wmod = self.module
+        orig_filters = wmod.filters
+        orig_showwarning = wmod.showwarning
+        # Ensure default behaviour is not to record warnings
+        with wmod.catch_warnings(module=wmod) as w:
+            self.assertTrue(w is None)
+            self.assertTrue(wmod.showwarning is orig_showwarning)
+            self.assertTrue(wmod.filters is not orig_filters)
+        self.assertTrue(wmod.filters is orig_filters)
+        if wmod is sys.modules['warnings']:
+            # Ensure the default module is this one
+            with wmod.catch_warnings() as w:
+                self.assertTrue(w is None)
+                self.assertTrue(wmod.showwarning is orig_showwarning)
+                self.assertTrue(wmod.filters is not orig_filters)
+            self.assertTrue(wmod.filters is orig_filters)
+
+    def test_check_warnings(self):
+        # Explicit tests for the test.support convenience wrapper
+        wmod = self.module
+        if wmod is not sys.modules['warnings']:
+            self.skipTest('module to test is not loaded warnings module')
+        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")
+
+class CCatchWarningTests(CatchWarningTests, unittest.TestCase):
+    module = c_warnings
+
+class PyCatchWarningTests(CatchWarningTests, unittest.TestCase):
+    module = py_warnings
+
+
+class EnvironmentVariableTests(BaseTest):
+
+    def test_single_warning(self):
+        rc, stdout, stderr = assert_python_ok("-c",
+            "import sys; sys.stdout.write(str(sys.warnoptions))",
+            PYTHONWARNINGS="ignore::DeprecationWarning")
+        self.assertEqual(stdout, b"['ignore::DeprecationWarning']")
+
+    def test_comma_separated_warnings(self):
+        rc, stdout, stderr = assert_python_ok("-c",
+            "import sys; sys.stdout.write(str(sys.warnoptions))",
+            PYTHONWARNINGS="ignore::DeprecationWarning,ignore::UnicodeWarning")
+        self.assertEqual(stdout,
+            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
+
+    def test_envvar_and_command_line(self):
+        rc, stdout, stderr = assert_python_ok("-Wignore::UnicodeWarning", "-c",
+            "import sys; sys.stdout.write(str(sys.warnoptions))",
+            PYTHONWARNINGS="ignore::DeprecationWarning")
+        self.assertEqual(stdout,
+            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
+
+    def test_conflicting_envvar_and_command_line(self):
+        rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
+            "import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
+            "warnings.warn('Message', DeprecationWarning)",
+            PYTHONWARNINGS="default::DeprecationWarning")
+        self.assertEqual(stdout,
+            b"['default::DeprecationWarning', 'error::DeprecationWarning']")
+        self.assertEqual(stderr.splitlines(),
+            [b"Traceback (most recent call last):",
+             b"  File \"<string>\", line 1, in <module>",
+             b"DeprecationWarning: Message"])
+
+    @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
+                         'requires non-ascii filesystemencoding')
+    def test_nonascii(self):
+        rc, stdout, stderr = assert_python_ok("-c",
+            "import sys; sys.stdout.write(str(sys.warnoptions))",
+            PYTHONIOENCODING="utf-8",
+            PYTHONWARNINGS="ignore:DeprecaciónWarning")
+        self.assertEqual(stdout,
+            "['ignore:DeprecaciónWarning']".encode('utf-8'))
+
+class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
+    module = c_warnings
+
+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
+        with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
+            # encodings loaded by initfsencoding()
+            assert_python_ok('-c', 'pass', PYTHONPATH=cwd)
+
+            # Use -W to load warnings module at startup
+            assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd)
+
+class FinalizationTest(unittest.TestCase):
+    def test_finalization(self):
+        # Issue #19421: warnings.warn() should not crash
+        # during Python finalization
+        code = """
+import warnings
+warn = warnings.warn
+
+class A:
+    def __del__(self):
+        warn("test")
+
+a=A()
+        """
+        rc, out, err = assert_python_ok("-c", code)
+        # note: "__main__" filename is not correct, it should be the name
+        # of the script
+        self.assertEqual(err, b'__main__:7: UserWarning: test')
+
+
+def setUpModule():
+    py_warnings.onceregistry.clear()
+    c_warnings.onceregistry.clear()
+
+tearDownModule = setUpModule
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/Lib/test/test_warnings/__main__.py b/Lib/test/test_warnings/__main__.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_warnings/__main__.py
@@ -0,0 +1,3 @@
+import unittest
+
+unittest.main('test.test_warnings')
diff --git a/Lib/test/test_warnings/data/import_warning.py b/Lib/test/test_warnings/data/import_warning.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_warnings/data/import_warning.py
@@ -0,0 +1,3 @@
+import warnings
+
+warnings.warn('module-level warning', DeprecationWarning, stacklevel=2)
\ No newline at end of file
diff --git a/Lib/test/warning_tests.py b/Lib/test/test_warnings/data/stacklevel.py
rename from Lib/test/warning_tests.py
rename to Lib/test/test_warnings/data/stacklevel.py
diff --git a/Lib/warnings.py b/Lib/warnings.py
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -160,6 +160,20 @@
     return cat
 
 
+def _is_internal_frame(frame):
+    """Signal whether the frame is an internal CPython implementation detail."""
+    filename = frame.f_code.co_filename
+    return 'importlib' in filename and '_bootstrap' in filename
+
+
+def _next_external_frame(frame):
+    """Find the next frame that doesn't involve CPython internals."""
+    frame = frame.f_back
+    while frame is not None and _is_internal_frame(frame):
+        frame = frame.f_back
+    return frame
+
+
 # Code typically replaced by _warnings
 def warn(message, category=None, stacklevel=1):
     """Issue a warning, or maybe ignore it or raise an exception."""
@@ -174,13 +188,23 @@
                         "not '{:s}'".format(type(category).__name__))
     # Get context information
     try:
-        caller = sys._getframe(stacklevel)
+        if stacklevel <= 1 or _is_internal_frame(sys._getframe(1)):
+            # If frame is too small to care or if the warning originated in
+            # internal code, then do not try to hide any frames.
+            frame = sys._getframe(stacklevel)
+        else:
+            frame = sys._getframe(1)
+            # Look for one frame less since the above line starts us off.
+            for x in range(stacklevel-1):
+                frame = _next_external_frame(frame)
+                if frame is None:
+                    raise ValueError
     except ValueError:
         globals = sys.__dict__
         lineno = 1
     else:
-        globals = caller.f_globals
-        lineno = caller.f_lineno
+        globals = frame.f_globals
+        lineno = frame.f_lineno
     if '__name__' in globals:
         module = globals['__name__']
     else:
@@ -374,7 +398,6 @@
     defaultaction = _defaultaction
     onceregistry = _onceregistry
     _warnings_defaults = True
-
 except ImportError:
     filters = []
     defaultaction = "default"
diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py
--- a/Lib/webbrowser.py
+++ b/Lib/webbrowser.py
@@ -495,23 +495,10 @@
 #
 
 if sys.platform[:3] == "win":
-
     class WindowsDefault(BaseBrowser):
-        # Windows Default opening arguments.
-
-        cmd = "start"
-        newwindow = ""
-        newtab = ""
-
         def open(self, url, new=0, autoraise=True):
-            # Format the command for optional arguments and add the url.
-            if new == 1:
-                self.cmd += " " + self.newwindow
-            elif new == 2:
-                self.cmd += " " + self.newtab
-            self.cmd += " " + url
             try:
-                subprocess.call(self.cmd, shell=True)
+                os.startfile(url)
             except OSError:
                 # [Error 22] No application is associated with the specified
                 # file for this operation: '<URL>'
@@ -519,108 +506,19 @@
             else:
                 return True
 
-
-    # Windows Sub-Classes for commonly used browsers.
-
-    class InternetExplorer(WindowsDefault):
-        """Launcher class for Internet Explorer browser"""
-
-        cmd = "start iexplore.exe"
-        newwindow = ""
-        newtab = ""
-
-
-    class WinChrome(WindowsDefault):
-        """Launcher class for windows specific Google Chrome browser"""
-
-        cmd = "start chrome.exe"
-        newwindow = "-new-window"
-        newtab = "-new-tab"
-
-
-    class WinFirefox(WindowsDefault):
-        """Launcher class for windows specific Firefox browser"""
-
-        cmd = "start firefox.exe"
-        newwindow = "-new-window"
-        newtab = "-new-tab"
-
-
-    class WinOpera(WindowsDefault):
-        """Launcher class for windows specific Opera browser"""
-
-        cmd = "start opera"
-        newwindow = ""
-        newtab = ""
-
-
-    class WinSeaMonkey(WindowsDefault):
-        """Launcher class for windows specific SeaMonkey browser"""
-
-        cmd = "start seamonkey"
-        newwinow = ""
-        newtab = ""
-
-
     _tryorder = []
     _browsers = {}
 
-    # First try to use the default Windows browser.
+    # First try to use the default Windows browser
     register("windows-default", WindowsDefault)
 
-    def find_windows_browsers():
-        """ Access the windows registry to determine
-        what browsers are on the system.
-        """
-
-        import winreg
-        HKLM = winreg.HKEY_LOCAL_MACHINE
-        subkey = r'Software\Clients\StartMenuInternet'
-        read32 = winreg.KEY_READ | winreg.KEY_WOW64_32KEY
-        read64 = winreg.KEY_READ | winreg.KEY_WOW64_64KEY
-        key32 = winreg.OpenKey(HKLM, subkey, access=read32)
-        key64 = winreg.OpenKey(HKLM, subkey, access=read64)
-
-        # Return a list of browsers found in the registry
-        # Check if there are any different browsers in the
-        # 32 bit location instead of the 64 bit location.
-        browsers = []
-        i = 0
-        while True:
-            try:
-                browsers.append(winreg.EnumKey(key32, i))
-            except EnvironmentError:
-                break
-            i += 1
-
-        i = 0
-        while True:
-            try:
-                browsers.append(winreg.EnumKey(key64, i))
-            except EnvironmentError:
-                break
-            i += 1
-
-        winreg.CloseKey(key32)
-        winreg.CloseKey(key64)
-
-        return browsers
-
-    # Detect some common windows browsers
-    for browser in find_windows_browsers():
-        browser = browser.lower()
-        if "iexplore" in browser:
-            register("iexplore", None, InternetExplorer("iexplore"))
-        elif "chrome" in browser:
-            register("chrome", None, WinChrome("chrome"))
-        elif "firefox" in browser:
-            register("firefox", None, WinFirefox("firefox"))
-        elif "opera" in browser:
-            register("opera", None, WinOpera("opera"))
-        elif "seamonkey" in browser:
-            register("seamonkey", None, WinSeaMonkey("seamonkey"))
-        else:
-            register(browser, None, WindowsDefault(browser))
+    # Detect some common Windows browsers, fallback to IE
+    iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
+                            "Internet Explorer\\IEXPLORE.EXE")
+    for browser in ("firefox", "firebird", "seamonkey", "mozilla",
+                    "netscape", "opera", iexplore):
+        if shutil.which(browser):
+            register(browser, None, BackgroundBrowser(browser))
 
 #
 # Platform support for MacOS
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1,4 +1,4 @@
-+++++++++++
++++++++++++
 Python News
 +++++++++++
 
@@ -88,6 +88,9 @@
 Core and Builtins
 -----------------
 
+- Issue #24305: Prevent import subsystem stack frames from being counted
+  by the warnings.warn(stacklevel=) parameter.
+
 - Issue #24912: Prevent __class__ assignment to immutable built-in objects.
 
 - Issue #24975: Fix AST compilation for PEP 448 syntax.
@@ -95,9 +98,15 @@
 Library
 -------
 
+- Issue #24917: time_strftime() buffer over-read.
 - Issue #23144: Make sure that HTMLParser.feed() returns all the data, even
   when convert_charrefs is True.
 
+- Issue #24748: To resolve a compatibility problem found with py2exe and
+  pywin32, imp.load_dynamic() once again ignores previously loaded modules
+  to support Python modules replacing themselves with extension modules.
+  Patch by Petr Viktorin.
+
 - Issue #24635: Fixed a bug in typing.py where isinstance([], typing.Iterable)
   would return True once, then False on subsequent calls.
 
@@ -386,9 +395,6 @@
 - Issue #14373: C implementation of functools.lru_cache() now can be used with
   methods.
 
-- Issue #8232: webbrowser support incomplete on Windows. Patch by Brandon
-  Milam
-
 - Issue #24347: Set KeyError if PyDict_GetItemWithError returns NULL.
 
 - Issue #24348: Drop superfluous incref/decref.
diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c
--- a/Modules/_testmultiphase.c
+++ b/Modules/_testmultiphase.c
@@ -582,3 +582,13 @@
 {
     return PyModuleDef_Init(&def_exec_unreported_exception);
 }
+
+/*** Helper for imp test ***/
+
+static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods);
+
+PyMODINIT_FUNC
+PyInit_imp_dummy(PyObject *spec)
+{
+    return PyModuleDef_Init(&imp_dummy_def);
+}
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -610,14 +610,15 @@
 
 #if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
     /* check that the format string contains only valid directives */
-    for(outbuf = strchr(fmt, '%');
+    for (outbuf = strchr(fmt, '%');
         outbuf != NULL;
         outbuf = strchr(outbuf+2, '%'))
     {
-        if (outbuf[1]=='#')
+        if (outbuf[1] == '#')
             ++outbuf; /* not documented by python, */
-        if ((outbuf[1] == 'y') && buf.tm_year < 0)
-        {
+        if (outbuf[1] == '\0')
+            break;
+        if ((outbuf[1] == 'y') && buf.tm_year < 0) {
             PyErr_SetString(PyExc_ValueError,
                         "format %y requires year >= 1900 on Windows");
             Py_DECREF(format);
@@ -625,10 +626,12 @@
         }
     }
 #elif (defined(_AIX) || defined(sun)) && defined(HAVE_WCSFTIME)
-    for(outbuf = wcschr(fmt, '%');
+    for (outbuf = wcschr(fmt, '%');
         outbuf != NULL;
         outbuf = wcschr(outbuf+2, '%'))
     {
+        if (outbuf[1] == L'\0')
+            break;
         /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0))
            returns "0/" instead of "99" */
         if (outbuf[1] == L'y' && buf.tm_year < 0) {
@@ -659,7 +662,8 @@
 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
         err = errno;
 #endif
-        if (buflen > 0 || i >= 256 * fmtlen) {
+        if (buflen > 0 || fmtlen == 0 ||
+            (fmtlen > 4 && i >= 256 * fmtlen)) {
             /* If the buffer is 256 times as long as the format,
                it's probably not failing for lack of room!
                More likely, the format yields an empty result,
diff --git a/Python/_warnings.c b/Python/_warnings.c
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -513,6 +513,64 @@
     return result;  /* Py_None or NULL. */
 }
 
+static int
+is_internal_frame(PyFrameObject *frame)
+{
+    static PyObject *importlib_string = NULL;
+    static PyObject *bootstrap_string = NULL;
+    PyObject *filename;
+    int contains;
+
+    if (importlib_string == NULL) {
+        importlib_string = PyUnicode_FromString("importlib");
+        if (importlib_string == NULL) {
+            return 0;
+        }
+
+        bootstrap_string = PyUnicode_FromString("_bootstrap");
+        if (bootstrap_string == NULL) {
+            Py_DECREF(importlib_string);
+            return 0;
+        }
+        Py_INCREF(importlib_string);
+        Py_INCREF(bootstrap_string);
+    }
+
+    if (frame == NULL || frame->f_code == NULL ||
+            frame->f_code->co_filename == NULL) {
+        return 0;
+    }
+    filename = frame->f_code->co_filename;
+    if (!PyUnicode_Check(filename)) {
+        return 0;
+    }
+    contains = PyUnicode_Contains(filename, importlib_string);
+    if (contains < 0) {
+        return 0;
+    }
+    else if (contains > 0) {
+        contains = PyUnicode_Contains(filename, bootstrap_string);
+        if (contains < 0) {
+            return 0;
+        }
+        else if (contains > 0) {
+            return 1;
+        }
+    }
+
+    return 0;
+}
+
+static PyFrameObject *
+next_external_frame(PyFrameObject *frame)
+{
+    do {
+        frame = frame->f_back;
+    } while (frame != NULL && is_internal_frame(frame));
+
+    return frame;
+}
+
 /* filename, module, and registry are new refs, globals is borrowed */
 /* Returns 0 on error (no new refs), 1 on success */
 static int
@@ -523,8 +581,18 @@
 
     /* Setup globals and lineno. */
     PyFrameObject *f = PyThreadState_GET()->frame;
-    while (--stack_level > 0 && f != NULL)
-        f = f->f_back;
+    // Stack level comparisons to Python code is off by one as there is no
+    // warnings-related stack level to avoid.
+    if (stack_level <= 0 || is_internal_frame(f)) {
+        while (--stack_level > 0 && f != NULL) {
+            f = f->f_back;
+        }
+    }
+    else {
+        while (--stack_level > 0 && f != NULL) {
+            f = next_external_frame(f);
+        }
+    }
 
     if (f == NULL) {
         globals = PyThreadState_Get()->interp->sysdict;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list