.. highlightlang:: none
+.. ATTENTION: You probably should update Misc/python.man, too, if you modify
+.. this file.
+
.. _using-on-general:
Command line and environment
:pep:`230` -- Warning framework
+ :envvar:`PYTHONWARNINGS`
+
.. cmdoption:: -x
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
~~~~~~~~~~~~~~~~~~~~
from io import StringIO
import sys
import unittest
+import subprocess
from test import support
from test import warning_tests
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()
_WarningsTests,
CWarningsDisplayTests, PyWarningsDisplayTests,
CCatchWarningTests, PyCatchWarningTests,
+ CEnvironmentVariableTests,
+ PyEnvironmentVariableTests
)
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 = "\
(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)
{