44from io import StringIO
55import sys
66import unittest
7+ import subprocess
78from test import support
89
910from 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+
688725def 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
0 commit comments