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

Skip to content

Commit 6999441

Browse files
committed
Issue #20355: -W command line options now have higher priority than the PYTHONWARNINGS environment variable. Patch by Arfrever.
1 parent 7fae75a commit 6999441

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

Lib/test/test_warnings.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import unittest
77
from test import support
8-
from test.script_helper import assert_python_ok
8+
from test.script_helper import assert_python_ok, assert_python_failure
99

1010
from test import warning_tests
1111

@@ -748,7 +748,19 @@ def test_envvar_and_command_line(self):
748748
"import sys; sys.stdout.write(str(sys.warnoptions))",
749749
PYTHONWARNINGS="ignore::DeprecationWarning")
750750
self.assertEqual(stdout,
751-
b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
751+
b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
752+
753+
def test_conflicting_envvar_and_command_line(self):
754+
rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
755+
"import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
756+
"warnings.warn('Message', DeprecationWarning)",
757+
PYTHONWARNINGS="default::DeprecationWarning")
758+
self.assertEqual(stdout,
759+
b"['default::DeprecationWarning', 'error::DeprecationWarning']")
760+
self.assertEqual(stderr.splitlines(),
761+
[b"Traceback (most recent call last):",
762+
b" File \"<string>\", line 1, in <module>",
763+
b"DeprecationWarning: Message"])
752764

753765
@unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
754766
'requires non-ascii filesystemencoding')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #20355: -W command line options now have higher priority than the
14+
PYTHONWARNINGS environment variable. Patch by Arfrever.
15+
1316
- Issue #21274: Define PATH_MAX for GNU/Hurd in Python/pythonrun.c.
1417

1518
- Issue #20904: Support setting FPU precision on m68k.

Modules/main.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ Py_Main(int argc, wchar_t **argv)
343343
int version = 0;
344344
int saw_unbuffered_flag = 0;
345345
PyCompilerFlags cf;
346+
PyObject *warning_option = NULL;
347+
PyObject *warning_options = NULL;
346348

347349
cf.cf_flags = 0;
348350

@@ -465,7 +467,15 @@ Py_Main(int argc, wchar_t **argv)
465467
break;
466468

467469
case 'W':
468-
PySys_AddWarnOption(_PyOS_optarg);
470+
if (warning_options == NULL)
471+
warning_options = PyList_New(0);
472+
if (warning_options == NULL)
473+
Py_FatalError("failure in handling of -W argument");
474+
warning_option = PyUnicode_FromWideChar(_PyOS_optarg, -1);
475+
if (warning_option == NULL)
476+
Py_FatalError("failure in handling of -W argument");
477+
PyList_Append(warning_options, warning_option);
478+
Py_DECREF(warning_option);
469479
break;
470480

471481
case 'X':
@@ -559,6 +569,12 @@ Py_Main(int argc, wchar_t **argv)
559569
PyMem_RawFree(buf);
560570
}
561571
#endif
572+
if (warning_options != NULL) {
573+
Py_ssize_t i;
574+
for (i = 0; i < PyList_GET_SIZE(warning_options); i++) {
575+
PySys_AddWarnOptionUnicode(PyList_GET_ITEM(warning_options, i));
576+
}
577+
}
562578

563579
if (command == NULL && module == NULL && _PyOS_optind < argc &&
564580
wcscmp(argv[_PyOS_optind], L"-") != 0)
@@ -652,6 +668,7 @@ Py_Main(int argc, wchar_t **argv)
652668
Py_SetProgramName(argv[0]);
653669
#endif
654670
Py_Initialize();
671+
Py_XDECREF(warning_options);
655672

656673
if (!Py_QuietFlag && (Py_VerboseFlag ||
657674
(command == NULL && filename == NULL &&

0 commit comments

Comments
 (0)