[Python-checkins] r86395 - in python/branches/py3k: Lib/test/test_warnings.py Misc/NEWS Python/pythonrun.c

antoine.pitrou python-checkins at python.org
Wed Nov 10 14:55:25 CET 2010


Author: antoine.pitrou
Date: Wed Nov 10 14:55:25 2010
New Revision: 86395

Log:
Issue #10372: Import the warnings module only after the IO library is
initialized, so as to avoid bootstrap issues with the '-W' option.



Modified:
   python/branches/py3k/Lib/test/test_warnings.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Python/pythonrun.c

Modified: python/branches/py3k/Lib/test/test_warnings.py
==============================================================================
--- python/branches/py3k/Lib/test/test_warnings.py	(original)
+++ python/branches/py3k/Lib/test/test_warnings.py	Wed Nov 10 14:55:25 2010
@@ -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<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(BaseTest, WCmdLineTests):
     module = c_warnings
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Nov 10 14:55:25 2010
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #10372: Import the warnings module only after the IO library is
+  initialized, so as to avoid bootstrap issues with the '-W' option.
+
 - Issue #10293: Remove obsolete field in the PyMemoryView structure,
   unused undocumented value PyBUF_SHADOW, and strangely-looking code in
   PyMemoryView_GetContiguous.

Modified: python/branches/py3k/Python/pythonrun.c
==============================================================================
--- python/branches/py3k/Python/pythonrun.c	(original)
+++ python/branches/py3k/Python/pythonrun.c	Wed Nov 10 14:55:25 2010
@@ -299,19 +299,21 @@
     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();
+        if (warnings_module == NULL) {
+            fprintf(stderr, "'import warnings' failed; traceback:\n");
+            PyErr_Print();
+        }
         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 */
 }


More information about the Python-checkins mailing list