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

Skip to content

Commit cf9f980

Browse files
committed
Issue #10372: Import the warnings module only after the IO library is
initialized, so as to avoid bootstrap issues with the '-W' option.
1 parent 81c87c5 commit cf9f980

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

Lib/test/test_warnings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import unittest
77
import subprocess
88
from test import support
9+
from test.script_helper import assert_python_ok
910

1011
from test import warning_tests
1112

@@ -393,6 +394,22 @@ def test_improper_input(self):
393394
self.module._setoption('error::Warning::0')
394395
self.assertRaises(UserWarning, self.module.warn, 'convert to error')
395396

397+
def test_improper_option(self):
398+
# Same as above, but check that the message is printed out when
399+
# the interpreter is executed. This also checks that options are
400+
# actually parsed at all.
401+
rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
402+
self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)
403+
404+
def test_warnings_bootstrap(self):
405+
# Check that the warnings module does get loaded when -W<some option>
406+
# is used (see issue #10372 for an example of silent bootstrap failure).
407+
rc, out, err = assert_python_ok("-Wi", "-c",
408+
"import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)")
409+
# '-Wi' was observed
410+
self.assertFalse(out.strip())
411+
self.assertNotIn(b'RuntimeWarning', err)
412+
396413
class CWCmdLineTests(BaseTest, WCmdLineTests):
397414
module = c_warnings
398415

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2 Alpha 4?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10372: Import the warnings module only after the IO library is
14+
initialized, so as to avoid bootstrap issues with the '-W' option.
15+
1316
- Issue #10293: Remove obsolete field in the PyMemoryView structure,
1417
unused undocumented value PyBUF_SHADOW, and strangely-looking code in
1518
PyMemoryView_GetContiguous.

Python/pythonrun.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,21 @@ Py_InitializeEx(int install_sigs)
299299
if (install_sigs)
300300
initsigs(); /* Signal handling stuff, including initintr() */
301301

302+
initmain(); /* Module __main__ */
303+
if (initstdio() < 0)
304+
Py_FatalError(
305+
"Py_Initialize: can't initialize sys standard streams");
306+
302307
/* Initialize warnings. */
303308
if (PySys_HasWarnOptions()) {
304309
PyObject *warnings_module = PyImport_ImportModule("warnings");
305-
if (!warnings_module)
306-
PyErr_Clear();
310+
if (warnings_module == NULL) {
311+
fprintf(stderr, "'import warnings' failed; traceback:\n");
312+
PyErr_Print();
313+
}
307314
Py_XDECREF(warnings_module);
308315
}
309316

310-
initmain(); /* Module __main__ */
311-
if (initstdio() < 0)
312-
Py_FatalError(
313-
"Py_Initialize: can't initialize sys standard streams");
314-
315317
if (!Py_NoSiteFlag)
316318
initsite(); /* Module site */
317319
}

0 commit comments

Comments
 (0)