Skip to content

Commit 7d79b8b

Browse files
author
Victor Stinner
committed
Issue #8766: Initialize _warnings module before importing the first module.
Fix a crash if an empty directory called "encodings" exists in sys.path.
1 parent 5b08b4d commit 7d79b8b

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

Lib/test/test_warnings.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -738,20 +738,38 @@ class PyEnvironmentVariableTests(EnvironmentVariableTests):
738738
module = py_warnings
739739

740740

741+
class BootstrapTest(unittest.TestCase):
742+
def test_issue_8766(self):
743+
# "import encodings" emits a warning whereas the warnings is not loaded
744+
# or not completly loaded (warnings imports indirectly encodings by
745+
# importing linecache) yet
746+
with support.temp_cwd() as cwd, support.temp_cwd('encodings'):
747+
env = os.environ.copy()
748+
env['PYTHONPATH'] = cwd
749+
750+
# encodings loaded by initfsencoding()
751+
retcode = subprocess.call([sys.executable, '-c', 'pass'], env=env)
752+
self.assertEqual(retcode, 0)
753+
754+
# Use -W to load warnings module at startup
755+
retcode = subprocess.call(
756+
[sys.executable, '-c', 'pass', '-W', 'always'],
757+
env=env)
758+
self.assertEqual(retcode, 0)
759+
741760
def test_main():
742761
py_warnings.onceregistry.clear()
743762
c_warnings.onceregistry.clear()
744-
support.run_unittest(CFilterTests,
745-
PyFilterTests,
746-
CWarnTests,
747-
PyWarnTests,
748-
CWCmdLineTests, PyWCmdLineTests,
749-
_WarningsTests,
750-
CWarningsDisplayTests, PyWarningsDisplayTests,
751-
CCatchWarningTests, PyCatchWarningTests,
752-
CEnvironmentVariableTests,
753-
PyEnvironmentVariableTests
754-
)
763+
support.run_unittest(
764+
CFilterTests, PyFilterTests,
765+
CWarnTests, PyWarnTests,
766+
CWCmdLineTests, PyWCmdLineTests,
767+
_WarningsTests,
768+
CWarningsDisplayTests, PyWarningsDisplayTests,
769+
CCatchWarningTests, PyCatchWarningTests,
770+
CEnvironmentVariableTests, PyEnvironmentVariableTests,
771+
BootstrapTest,
772+
)
755773

756774

757775
if __name__ == "__main__":

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #8766: Initialize _warnings module before importing the first module.
16+
Fix a crash if an empty directory called "encodings" exists in sys.path.
17+
1518
- Issue #8589: Decode PYTHONWARNINGS environment variable with the file system
1619
encoding and surrogateespace error handler instead of the locale encoding to
1720
be consistent with os.environ. Add PySys_AddWarnOptionUnicode() function.

Python/_warnings.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
116116
_filters = warnings_filters;
117117
}
118118

119-
if (!PyList_Check(_filters)) {
119+
if (_filters == NULL || !PyList_Check(_filters)) {
120120
PyErr_SetString(PyExc_ValueError,
121121
MODULE_NAME ".filters must be a list");
122122
return NULL;

Python/pythonrun.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ Py_InitializeEx(int install_sigs)
265265

266266
_PyImportHooks_Init();
267267

268+
/* Initialize _warnings. */
269+
_PyWarnings_Init();
270+
268271
initfsencoding();
269272

270273
if (install_sigs)
271274
initsigs(); /* Signal handling stuff, including initintr() */
272275

273276
/* Initialize warnings. */
274-
_PyWarnings_Init();
275277
if (PySys_HasWarnOptions()) {
276278
PyObject *warnings_module = PyImport_ImportModule("warnings");
277279
if (!warnings_module)

0 commit comments

Comments
 (0)