Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (révision 81326) +++ Python/pythonrun.c (copie de travail) @@ -265,13 +265,15 @@ _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) Index: Python/_warnings.c =================================================================== --- Python/_warnings.c (révision 81326) +++ Python/_warnings.c (copie de travail) @@ -116,7 +116,7 @@ _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: Lib/test/test_warnings.py =================================================================== --- Lib/test/test_warnings.py (révision 81326) +++ Lib/test/test_warnings.py (copie de travail) @@ -738,20 +738,38 @@ 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__":