Skip to content

Commit 0805ca3

Browse files
committed
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 ........
1 parent 42dfeec commit 0805ca3

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

Doc/using/cmdline.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.. highlightlang:: none
22

3+
.. ATTENTION: You probably should update Misc/python.man, too, if you modify
4+
.. this file.
5+
36
.. _using-on-general:
47

58
Command line and environment
@@ -306,6 +309,8 @@ Miscellaneous options
306309

307310
:pep:`230` -- Warning framework
308311

312+
:envvar:`PYTHONWARNINGS`
313+
309314

310315
.. cmdoption:: -x
311316

@@ -469,6 +474,12 @@ These environment variables influence Python's behavior.
469474
value instead of the value got through the C runtime. Only works on
470475
Mac OS X.
471476

477+
.. envvar:: PYTHONWARNINGS
478+
479+
This is the equivalent to the :option:`-W` option. If set to a comma
480+
separated string, it is equivalent to specifying :option:`-W` multiple
481+
times.
482+
472483

473484
Debug-mode variables
474485
~~~~~~~~~~~~~~~~~~~~

Lib/test/test_warnings.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from io import StringIO
55
import sys
66
import unittest
7+
import subprocess
78
from test import support
89

910
from test import warning_tests
@@ -685,6 +686,42 @@ class PyCatchWarningTests(CatchWarningTests):
685686
module = py_warnings
686687

687688

689+
class EnvironmentVariableTests(BaseTest):
690+
691+
def test_single_warning(self):
692+
newenv = os.environ.copy()
693+
newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
694+
p = subprocess.Popen([sys.executable,
695+
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
696+
stdout=subprocess.PIPE, env=newenv)
697+
self.assertEqual(p.stdout.read(), b"['ignore::DeprecationWarning']")
698+
699+
def test_comma_separated_warnings(self):
700+
newenv = os.environ.copy()
701+
newenv["PYTHONWARNINGS"] = ("ignore::DeprecationWarning,"
702+
"ignore::UnicodeWarning")
703+
p = subprocess.Popen([sys.executable,
704+
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
705+
stdout=subprocess.PIPE, env=newenv)
706+
self.assertEqual(p.stdout.read(),
707+
b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")
708+
709+
def test_envvar_and_command_line(self):
710+
newenv = os.environ.copy()
711+
newenv["PYTHONWARNINGS"] = "ignore::DeprecationWarning"
712+
p = subprocess.Popen([sys.executable, "-W" "ignore::UnicodeWarning",
713+
"-c", "import sys; sys.stdout.write(str(sys.warnoptions))"],
714+
stdout=subprocess.PIPE, env=newenv)
715+
self.assertEqual(p.stdout.read(),
716+
b"['ignore::UnicodeWarning', 'ignore::DeprecationWarning']")
717+
718+
class CEnvironmentVariableTests(EnvironmentVariableTests):
719+
module = c_warnings
720+
721+
class PyEnvironmentVariableTests(EnvironmentVariableTests):
722+
module = py_warnings
723+
724+
688725
def test_main():
689726
py_warnings.onceregistry.clear()
690727
c_warnings.onceregistry.clear()
@@ -696,6 +733,8 @@ def test_main():
696733
_WarningsTests,
697734
CWarningsDisplayTests, PyWarningsDisplayTests,
698735
CCatchWarningTests, PyCatchWarningTests,
736+
CEnvironmentVariableTests,
737+
PyEnvironmentVariableTests
699738
)
700739

701740

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7301: Add environment variable $PYTHONWARNINGS.
16+
1517
- Issue #8329: Don't return the same lists from select.select when no fds are
1618
changed.
1719

Misc/python.man

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ the \fB\-u\fP option.
394394
If this is set to a non-empty string it is equivalent to specifying
395395
the \fB\-v\fP option. If set to an integer, it is equivalent to
396396
specifying \fB\-v\fP multiple times.
397+
.IP PYTHONWARNINGS
398+
If this is set to a comma-separated string it is equivalent to
399+
specifying the \fB\-W\fP option for each separate value.
397400
.SH AUTHOR
398401
The Python Software Foundation: http://www.python.org/psf
399402
.SH INTERNET RESOURCES

Modules/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static char *usage_3 = "\
8282
can be supplied multiple times to increase verbosity\n\
8383
-V : print the Python version number and exit (also --version)\n\
8484
-W arg : warning control; arg is action:message:category:module:lineno\n\
85+
also PYTHONWARNINGS=arg\n\
8586
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
8687
";
8788
static char *usage_4 = "\
@@ -401,6 +402,24 @@ Py_Main(int argc, wchar_t **argv)
401402
(p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
402403
Py_NoUserSiteDirectory = 1;
403404

405+
if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
406+
char *buf;
407+
wchar_t *warning;
408+
size_t len;
409+
410+
for (buf = strtok(p, ",");
411+
buf != NULL;
412+
buf = strtok(NULL, ",")) {
413+
len = strlen(buf);
414+
warning = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
415+
if (warning == NULL)
416+
Py_FatalError(
417+
"not enough memory to copy PYTHONWARNINGS");
418+
mbstowcs(warning, buf, len);
419+
PySys_AddWarnOption(warning);
420+
}
421+
}
422+
404423
if (command == NULL && module == NULL && _PyOS_optind < argc &&
405424
wcscmp(argv[_PyOS_optind], L"-") != 0)
406425
{

0 commit comments

Comments
 (0)