[Python-checkins] r79881 - in python/branches/py3k: Doc/using/cmdline.rst Lib/test/test_warnings.py Misc/NEWS Misc/python.man Modules/main.c

philip.jenvey python-checkins at python.org
Wed Apr 7 06:04:10 CEST 2010


Author: philip.jenvey
Date: Wed Apr  7 06:04:10 2010
New Revision: 79881

Log:
Merged revisions 79878-79880 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79878 | philip.jenvey | 2010-04-06 16:24:45 -0700 (Tue, 06 Apr 2010) | 4 lines
  
  #7301: add the environment variable $PYTHONWARNINGS to supplement the -W
  command line option
  patch from Brian Curtin
........
  r79879 | benjamin.peterson | 2010-04-06 16:32:27 -0700 (Tue, 06 Apr 2010) | 1 line
  
  tell people to update python.man, too
........
  r79880 | philip.jenvey | 2010-04-06 16:38:57 -0700 (Tue, 06 Apr 2010) | 1 line
  
  document new PYTHONWARNINGS env var
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Doc/using/cmdline.rst
   python/branches/py3k/Lib/test/test_warnings.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Misc/python.man
   python/branches/py3k/Modules/main.c

Modified: python/branches/py3k/Doc/using/cmdline.rst
==============================================================================
--- python/branches/py3k/Doc/using/cmdline.rst	(original)
+++ python/branches/py3k/Doc/using/cmdline.rst	Wed Apr  7 06:04:10 2010
@@ -1,5 +1,8 @@
 .. highlightlang:: none
 
+.. ATTENTION: You probably should update Misc/python.man, too, if you modify
+.. this file.
+
 .. _using-on-general:
 
 Command line and environment
@@ -306,6 +309,8 @@
 
       :pep:`230` -- Warning framework
 
+      :envvar:`PYTHONWARNINGS`
+
 
 .. cmdoption:: -x
 
@@ -469,6 +474,12 @@
    value instead of the value got through the C runtime.  Only works on
    Mac OS X.
 
+.. envvar:: PYTHONWARNINGS
+
+   This is the equivalent to the :option:`-W` option. If set to a comma
+   separated string, it is equivalent to specifying :option:`-W` multiple
+   times.
+
 
 Debug-mode variables
 ~~~~~~~~~~~~~~~~~~~~

Modified: python/branches/py3k/Lib/test/test_warnings.py
==============================================================================
--- python/branches/py3k/Lib/test/test_warnings.py	(original)
+++ python/branches/py3k/Lib/test/test_warnings.py	Wed Apr  7 06:04:10 2010
@@ -4,6 +4,7 @@
 from io import StringIO
 import sys
 import unittest
+import subprocess
 from test import support
 
 from test import warning_tests
@@ -685,6 +686,42 @@
     module = py_warnings
 
 
+class EnvironmentVariableTests(BaseTest):
+
+    def test_single_warning(self):
+        newenv = os.environ.copy()
+        newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
+        p = subprocess.Popen([sys.executable,
+                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
+                stdout=subprocess.PIPE, env=newenv)
+        self.assertEqual(p.stdout.read(), b"['ignore::DeprecationWarning']")
+
+    def test_comma_separated_warnings(self):
+        newenv = os.environ.copy()
+        newenv["PYTHONWARNINGS"] = ("ignore::DeprecationWarning,"
+                                    "ignore::UnicodeWarning")
+        p = subprocess.Popen([sys.executable,
+                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
+                stdout=subprocess.PIPE, env=newenv)
+        self.assertEqual(p.stdout.read(),
+                b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
+
+    def test_envvar_and_command_line(self):
+        newenv = os.environ.copy()
+        newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
+        p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
+                "-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
+                stdout=subprocess.PIPE, env=newenv)
+        self.assertEqual(p.stdout.read(),
+                b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
+
+class CEnvironmentVariableTests(EnvironmentVariableTests):
+    module = c_warnings
+
+class PyEnvironmentVariableTests(EnvironmentVariableTests):
+    module = py_warnings
+
+
 def test_main():
     py_warnings.onceregistry.clear()
     c_warnings.onceregistry.clear()
@@ -696,6 +733,8 @@
                                 _WarningsTests,
                                 CWarningsDisplayTests, PyWarningsDisplayTests,
                                 CCatchWarningTests, PyCatchWarningTests,
+                                CEnvironmentVariableTests,
+                                PyEnvironmentVariableTests
                              )
 
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Apr  7 06:04:10 2010
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Issue #7301: Add environment variable $PYTHONWARNINGS.
+
 - Issue #8329: Don't return the same lists from select.select when no fds are
   changed.
 

Modified: python/branches/py3k/Misc/python.man
==============================================================================
--- python/branches/py3k/Misc/python.man	(original)
+++ python/branches/py3k/Misc/python.man	Wed Apr  7 06:04:10 2010
@@ -394,6 +394,9 @@
 If this is set to a non-empty string it is equivalent to specifying
 the \fB\-v\fP option. If set to an integer, it is equivalent to
 specifying \fB\-v\fP multiple times. 
+.IP PYTHONWARNINGS
+If this is set to a comma-separated string it is equivalent to
+specifying the \fB\-W\fP option for each separate value.
 .SH AUTHOR
 The Python Software Foundation: http://www.python.org/psf
 .SH INTERNET RESOURCES

Modified: python/branches/py3k/Modules/main.c
==============================================================================
--- python/branches/py3k/Modules/main.c	(original)
+++ python/branches/py3k/Modules/main.c	Wed Apr  7 06:04:10 2010
@@ -82,6 +82,7 @@
          can be supplied multiple times to increase verbosity\n\
 -V     : print the Python version number and exit (also --version)\n\
 -W arg : warning control; arg is action:message:category:module:lineno\n\
+         also PYTHONWARNINGS=arg\n\
 -x     : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
 ";
 static char *usage_4 = "\
@@ -401,6 +402,24 @@
 	    (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
 		Py_NoUserSiteDirectory = 1;
 
+	if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
+		char *buf;
+		wchar_t *warning;
+		size_t len;
+
+		for (buf = strtok(p, ",");
+		     buf != NULL;
+		     buf = strtok(NULL, ",")) {
+			len = strlen(buf);
+			warning = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
+			if (warning == NULL)
+				Py_FatalError(
+				   "not enough memory to copy PYTHONWARNINGS");
+			mbstowcs(warning, buf, len);
+			PySys_AddWarnOption(warning);
+		}
+	}
+
 	if (command == NULL && module == NULL && _PyOS_optind < argc &&
 	    wcscmp(argv[_PyOS_optind], L"-") != 0)
 	{


More information about the Python-checkins mailing list