Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 148051a

Browse files
author
Victor Stinner
committed
Recorded merge of revisions 81364 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent 784c027 commit 148051a

4 files changed

Lines changed: 47 additions & 11 deletions

File tree

Lib/test/test_warnings.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from io import StringIO
55
import sys
66
import unittest
7+
import shutil
8+
import tempfile
9+
import subprocess
710
from test import support
811

912
from test import warning_tests
@@ -670,18 +673,46 @@ class PyCatchWarningTests(CatchWarningTests):
670673
module = py_warnings
671674

672675

676+
class BootstrapTest(unittest.TestCase):
677+
def test_issue_8766(self):
678+
# "import encodings" emits a warning whereas the warnings is not loaded
679+
# or not completly loaded (warnings imports indirectly encodings by
680+
# importing linecache) yet
681+
old_cwd = os.getcwd()
682+
try:
683+
cwd = tempfile.mkdtemp()
684+
try:
685+
os.chdir(cwd)
686+
os.mkdir('encodings')
687+
env = os.environ.copy()
688+
env['PYTHONPATH'] = cwd
689+
690+
# encodings loaded by initfsencoding()
691+
retcode = subprocess.call([sys.executable, '-c', 'pass'], env=env)
692+
self.assertEqual(retcode, 0)
693+
694+
# Use -W to load warnings module at startup
695+
retcode = subprocess.call(
696+
[sys.executable, '-c', 'pass', '-W', 'always'],
697+
env=env)
698+
self.assertEqual(retcode, 0)
699+
finally:
700+
shutil.rmtree(cwd)
701+
finally:
702+
os.chdir(old_cwd)
703+
673704
def test_main():
674705
py_warnings.onceregistry.clear()
675706
c_warnings.onceregistry.clear()
676-
support.run_unittest(CFilterTests,
677-
PyFilterTests,
678-
CWarnTests,
679-
PyWarnTests,
680-
CWCmdLineTests, PyWCmdLineTests,
681-
_WarningsTests,
682-
CWarningsDisplayTests, PyWarningsDisplayTests,
683-
CCatchWarningTests, PyCatchWarningTests,
684-
)
707+
support.run_unittest(
708+
CFilterTests, PyFilterTests,
709+
CWarnTests, PyWarnTests,
710+
CWCmdLineTests, PyWCmdLineTests,
711+
_WarningsTests,
712+
CWarningsDisplayTests, PyWarningsDisplayTests,
713+
CCatchWarningTests, PyCatchWarningTests,
714+
BootstrapTest,
715+
)
685716

686717

687718
if __name__ == "__main__":

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1.3?
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
- PyObject_Dump() encodes unicode objects to utf8 with backslashreplace
1619
(instead of strict) error handler to escape surrogates
1720

Python/_warnings.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ Py_InitializeEx(int install_sigs)
264264

265265
_PyImportHooks_Init();
266266

267+
/* Initialize _warnings. */
268+
_PyWarnings_Init();
269+
267270
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
268271
/* On Unix, set the file system encoding according to the
269272
user's preference, if the CODESET names a well-known
@@ -284,7 +287,6 @@ Py_InitializeEx(int install_sigs)
284287
initsigs(); /* Signal handling stuff, including initintr() */
285288

286289
/* Initialize warnings. */
287-
_PyWarnings_Init();
288290
if (PySys_HasWarnOptions()) {
289291
PyObject *warnings_module = PyImport_ImportModule("warnings");
290292
if (!warnings_module)

0 commit comments

Comments
 (0)