From 7d79b8b7714b5e7d8a0582a07b5625c280c879c0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 19 May 2010 20:40:50 +0000 Subject: 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 | 40 +++++++++++++++++++++++++++++----------- Misc/NEWS | 3 +++ Python/_warnings.c | 2 +- Python/pythonrun.c | 4 +++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index 5f6a670..52b9dbe 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -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__": diff --git a/Misc/NEWS b/Misc/NEWS index 3e582cd..e68e5da 100644 --- 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. diff --git a/Python/_warnings.c b/Python/_warnings.c index c49f3f3..b1b2b71 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -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; diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 58388c2..b469c4a 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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) -- cgit v0.12