--- Lib/test/test_warnings.py +++ Lib/test/test_warnings.py @@ -758,9 +758,25 @@ "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"], stdout=subprocess.PIPE, env=newenv) self.assertEqual(p.communicate()[0], - b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']") + b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']") self.assertEqual(p.wait(), 0) + def test_conflicting_envvar_and_command_line(self): + newenv = os.environ.copy() + newenv["PYTHONWARNINGS"] = "default::DeprecationWarning" + p = subprocess.Popen([sys.executable, "-W" "error::DeprecationWarning", + "-c", "import sys, warnings; sys.stdout.write(str(sys.warnoptions));" + "warnings.warn('Message', DeprecationWarning)"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=newenv) + stdout, stderr = p.communicate() + self.assertEqual(stdout, + b"['default::DeprecationWarning', 'error::DeprecationWarning']") + self.assertEqual(stderr, + b"Traceback (most recent call last):\n" + b" File \"\", line 1, in \n" + b"DeprecationWarning: Message\n") + self.assertEqual(p.wait(), 1) + @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii', 'requires non-ascii filesystemencoding') def test_nonascii(self): --- Modules/main.c +++ Modules/main.c @@ -343,6 +343,8 @@ int version = 0; int saw_unbuffered_flag = 0; PyCompilerFlags cf; + PyObject *warning_option = NULL; + PyObject *warning_options = NULL; cf.cf_flags = 0; @@ -465,7 +467,15 @@ break; case 'W': - PySys_AddWarnOption(_PyOS_optarg); + if (warning_options == NULL) + warning_options = PyList_New(0); + if (warning_options == NULL) + Py_FatalError("failure in handling of -W argument"); + warning_option = PyUnicode_FromWideChar(_PyOS_optarg, -1); + if (warning_option == NULL) + Py_FatalError("failure in handling of -W argument"); + PyList_Append(warning_options, warning_option); + Py_DECREF(warning_option); break; case 'X': @@ -559,6 +569,12 @@ PyMem_RawFree(buf); } #endif + if (warning_options != NULL) { + Py_ssize_t i; + for (i = 0; i < PyList_GET_SIZE(warning_options); i++) { + PySys_AddWarnOptionUnicode(PyList_GET_ITEM(warning_options, i)); + } + } if (command == NULL && module == NULL && _PyOS_optind < argc && wcscmp(argv[_PyOS_optind], L"-") != 0)