]> granicus.if.org Git - python/commitdiff
Recorded merge of revisions 81364 via svnmerge from
authorVictor Stinner <victor.stinner@haypocalc.com>
Thu, 20 May 2010 21:00:34 +0000 (21:00 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Thu, 20 May 2010 21:00:34 +0000 (21:00 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r81364 | victor.stinner | 2010-05-19 22:40:50 +0200 (mer., 19 mai 2010) | 3 lines

  Issue #8766: Initialize _warnings module before importing the first module.
  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 c1e29b87fcef0ba491a11637d86a01409c6d0f4c..5cf1034a5da524f7e1f97ed6771ae89b272510cb 100644 (file)
@@ -4,6 +4,9 @@ import os
 from io import StringIO
 import sys
 import unittest
+import shutil
+import tempfile
+import subprocess
 from test import support
 
 from test import warning_tests
@@ -670,18 +673,46 @@ class PyCatchWarningTests(CatchWarningTests):
     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
+        old_cwd = os.getcwd()
+        try:
+            cwd = tempfile.mkdtemp()
+            try:
+                os.chdir(cwd)
+                os.mkdir('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)
+            finally:
+                shutil.rmtree(cwd)
+        finally:
+            os.chdir(old_cwd)
+
 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,
-                             )
+    support.run_unittest(
+        CFilterTests, PyFilterTests,
+        CWarnTests, PyWarnTests,
+        CWCmdLineTests, PyWCmdLineTests,
+        _WarningsTests,
+        CWarningsDisplayTests, PyWarningsDisplayTests,
+        CCatchWarningTests, PyCatchWarningTests,
+        BootstrapTest,
+    )
 
 
 if __name__ == "__main__":
index cf23427519fd37ff2b52480181d4b94b346ac737..66433e4f01831b65553927e1718865df579676b2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.1.3?
 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.
+
 - PyObject_Dump() encodes unicode objects to utf8 with backslashreplace
   (instead of strict) error handler to escape surrogates
 
index f0e1e512f53305b1a7164b3b8483aa17c7debc52..94538443c07ce09ca55f5e919eb13e51f998f036 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 ec2c6caea3fd87b3419a09e4a3a0376412407c39..252b2d171b80ebab743302d55de999617fde20b1 100644 (file)
@@ -264,6 +264,9 @@ Py_InitializeEx(int install_sigs)
 
     _PyImportHooks_Init();
 
+    /* Initialize _warnings. */
+    _PyWarnings_Init();
+
 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
     /* On Unix, set the file system encoding according to the
        user's preference, if the CODESET names a well-known
@@ -284,7 +287,6 @@ Py_InitializeEx(int install_sigs)
         initsigs(); /* Signal handling stuff, including initintr() */
 
     /* Initialize warnings. */
-    _PyWarnings_Init();
     if (PySys_HasWarnOptions()) {
         PyObject *warnings_module = PyImport_ImportModule("warnings");
         if (!warnings_module)