]> granicus.if.org Git - python/commitdiff
Issue #8766: Initialize _warnings module before importing the first module.
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 19 May 2010 20:40:50 +0000 (20:40 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 19 May 2010 20:40:50 +0000 (20:40 +0000)
Fix a crash if an empty directory called "encodings" exists in sys.path.

Lib/test/test_warnings.py
Misc/NEWS
Python/_warnings.c
Python/pythonrun.c

index 5f6a67029c5f38f66bbaae5213fd17b8bf3c138e..52b9dbedd77061ba3edb82174cd3c28c4fd942b4 100644 (file)
@@ -738,20 +738,38 @@ class PyEnvironmentVariableTests(EnvironmentVariableTests):
     module = py_warnings
 
 
+class BootstrapTest(unittest.TestCase):
+    def test_issue_8766(self):
+        # "import encodings" emits a warning whereas the warnings is not loaded
+        # or not completly loaded (warnings imports indirectly encodings by
+        # importing linecache) yet
+        with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
+            env = os.environ.copy()
+            env['PYTHONPATH'] = cwd
+
+            # encodings loaded by initfsencoding()
+            retcode = subprocess.call([sys.executable, '-c', 'pass'], env=env)
+            self.assertEqual(retcode, 0)
+
+            # Use -W to load warnings module at startup
+            retcode = subprocess.call(
+                [sys.executable, '-c', 'pass', '-W', 'always'],
+                env=env)
+            self.assertEqual(retcode, 0)
+
 def test_main():
     py_warnings.onceregistry.clear()
     c_warnings.onceregistry.clear()
-    support.run_unittest(CFilterTests,
-                                PyFilterTests,
-                                CWarnTests,
-                                PyWarnTests,
-                                CWCmdLineTests, PyWCmdLineTests,
-                                _WarningsTests,
-                                CWarningsDisplayTests, PyWarningsDisplayTests,
-                                CCatchWarningTests, PyCatchWarningTests,
-                                CEnvironmentVariableTests,
-                                PyEnvironmentVariableTests
-                             )
+    support.run_unittest(
+        CFilterTests, PyFilterTests,
+        CWarnTests, PyWarnTests,
+        CWCmdLineTests, PyWCmdLineTests,
+        _WarningsTests,
+        CWarningsDisplayTests, PyWarningsDisplayTests,
+        CCatchWarningTests, PyCatchWarningTests,
+        CEnvironmentVariableTests, PyEnvironmentVariableTests,
+        BootstrapTest,
+    )
 
 
 if __name__ == "__main__":
index 3e582cdbc584b0c57838ed1c2b3383e5c13f6efc..e68e5da1fb3cfaa64d7359f6d2adb8fe90933027 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #8766: Initialize _warnings module before importing the first module.
+  Fix a crash if an empty directory called "encodings" exists in sys.path.
+
 - Issue #8589: Decode PYTHONWARNINGS environment variable with the file system
   encoding and surrogateespace error handler instead of the locale encoding to
   be consistent with os.environ. Add PySys_AddWarnOptionUnicode() function.
index c49f3f32239d2b5d2122693f834bc3b4fda555e2..b1b2b71b8d4c662884d85f844ccf8c92c28e41da 100644 (file)
@@ -116,7 +116,7 @@ get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
         _filters = warnings_filters;
     }
 
-    if (!PyList_Check(_filters)) {
+    if (_filters == NULL || !PyList_Check(_filters)) {
         PyErr_SetString(PyExc_ValueError,
                         MODULE_NAME ".filters must be a list");
         return NULL;
index 58388c22d90c2f8a8eb8f65ed04d15b65ca3c10d..b469c4a625f1f35a436a4624233bce15cf922ec3 100644 (file)
@@ -265,13 +265,15 @@ Py_InitializeEx(int install_sigs)
 
     _PyImportHooks_Init();
 
+    /* Initialize _warnings. */
+    _PyWarnings_Init();
+
     initfsencoding();
 
     if (install_sigs)
         initsigs(); /* Signal handling stuff, including initintr() */
 
     /* Initialize warnings. */
-    _PyWarnings_Init();
     if (PySys_HasWarnOptions()) {
         PyObject *warnings_module = PyImport_ImportModule("warnings");
         if (!warnings_module)