[Python-checkins] cpython: Issue #20355: -W command line options now have higher priority than the

antoine.pitrou python-checkins at python.org
Tue Apr 29 00:58:20 CEST 2014


http://hg.python.org/cpython/rev/78c0d80a04f9
changeset:   90488:78c0d80a04f9
parent:      90486:1fd9c3f6cf68
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Tue Apr 29 00:56:08 2014 +0200
summary:
  Issue #20355: -W command line options now have higher priority than the PYTHONWARNINGS environment variable.  Patch by Arfrever.

files:
  Lib/test/test_warnings.py |  16 ++++++++++++++--
  Misc/NEWS                 |   3 +++
  Modules/main.c            |  19 ++++++++++++++++++-
  3 files changed, 35 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -5,7 +5,7 @@
 import sys
 import unittest
 from test import support
-from test.script_helper import assert_python_ok
+from test.script_helper import assert_python_ok, assert_python_failure
 
 from test import warning_tests
 
@@ -748,7 +748,19 @@
             "import sys; sys.stdout.write(str(sys.warnoptions))",
             PYTHONWARNINGS="ignore::DeprecationWarning")
         self.assertEqual(stdout,
-            b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
+            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
+
+    def test_conflicting_envvar_and_command_line(self):
+        rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
+            "import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
+            "warnings.warn('Message', DeprecationWarning)",
+            PYTHONWARNINGS="default::DeprecationWarning")
+        self.assertEqual(stdout,
+            b"['default::DeprecationWarning', 'error::DeprecationWarning']")
+        self.assertEqual(stderr.splitlines(),
+            [b"Traceback (most recent call last):",
+             b"  File \"<string>\", line 1, in <module>",
+             b"DeprecationWarning: Message"])
 
     @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
                          'requires non-ascii filesystemencoding')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #20355: -W command line options now have higher priority than the
+  PYTHONWARNINGS environment variable.  Patch by Arfrever.
+
 - Issue #21274: Define PATH_MAX for GNU/Hurd in Python/pythonrun.c.
 
 - Issue #20904: Support setting FPU precision on m68k.
diff --git a/Modules/main.c b/Modules/main.c
--- a/Modules/main.c
+++ b/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)
@@ -652,6 +668,7 @@
     Py_SetProgramName(argv[0]);
 #endif
     Py_Initialize();
+    Py_XDECREF(warning_options);
 
     if (!Py_QuietFlag && (Py_VerboseFlag ||
                         (command == NULL && filename == NULL &&

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list