Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (révision 86361) +++ Python/pythonrun.c (copie de travail) @@ -299,19 +299,19 @@ if (install_sigs) initsigs(); /* Signal handling stuff, including initintr() */ + initmain(); /* Module __main__ */ + if (initstdio() < 0) + Py_FatalError( + "Py_Initialize: can't initialize sys standard streams"); + /* Initialize warnings. */ if (PySys_HasWarnOptions()) { PyObject *warnings_module = PyImport_ImportModule("warnings"); if (!warnings_module) - PyErr_Clear(); + PyErr_WriteUnraisable(NULL); Py_XDECREF(warnings_module); } - initmain(); /* Module __main__ */ - if (initstdio() < 0) - Py_FatalError( - "Py_Initialize: can't initialize sys standard streams"); - if (!Py_NoSiteFlag) initsite(); /* Module site */ } Index: Lib/test/test_warnings.py =================================================================== --- Lib/test/test_warnings.py (révision 86361) +++ Lib/test/test_warnings.py (copie de travail) @@ -6,6 +6,7 @@ import unittest import subprocess from test import support +from test.script_helper import assert_python_ok from test import warning_tests @@ -393,6 +394,22 @@ 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 + # is used (see issue #10372 for an example of silent bootstrap failure). + rc, out, err = assert_python_ok("-Wi", "-c", + "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)") + # '-Wi' was observed + self.assertFalse(out.strip()) + self.assertNotIn(b'RuntimeWarning', err) + class CWCmdLineTests(BaseTest, WCmdLineTests): module = c_warnings Index: Lib/test/script_helper.py =================================================================== --- Lib/test/script_helper.py (révision 86361) +++ Lib/test/script_helper.py (copie de travail) @@ -34,9 +34,17 @@ return rc, out, err def assert_python_ok(*args): + """ + Assert that running the interpreter with `args` is ok and return a + (return code, stdout, stderr) tuple. + """ return _assert_python(True, *args) def assert_python_failure(*args): + """ + Assert that running the interpreter with `args` fails and return a + (return code, stdout, stderr) tuple. + """ return _assert_python(False, *args) def spawn_python(*args):