@@ -1300,6 +1300,46 @@ def test_threading_excepthook(self):
13001300 self .assertIn ("Warning -- Uncaught thread exception" , output )
13011301 self .assertIn ("Exception: bug in thread" , output )
13021302
1303+ def test_print_warning (self ):
1304+ # bpo-45410: The order of messages must be preserved when -W and
1305+ # support.print_warning() are used.
1306+ code = textwrap .dedent (r"""
1307+ import sys
1308+ import unittest
1309+ from test import support
1310+
1311+ class MyObject:
1312+ pass
1313+
1314+ def func_bug():
1315+ raise Exception("bug in thread")
1316+
1317+ class Tests(unittest.TestCase):
1318+ def test_print_warning(self):
1319+ print("msg1: stdout")
1320+ support.print_warning("msg2: print_warning")
1321+ # Fail with ENV CHANGED to see print_warning() log
1322+ support.environment_altered = True
1323+ """ )
1324+ testname = self .create_test (code = code )
1325+
1326+ # Expect an output like:
1327+ #
1328+ # test_threading_excepthook (test.test_x.Tests) ... msg1: stdout
1329+ # Warning -- msg2: print_warning
1330+ # ok
1331+ regex = (r"test_print_warning.*msg1: stdout\n"
1332+ r"Warning -- msg2: print_warning\n"
1333+ r"ok\n" )
1334+ for option in ("-v" , "-W" ):
1335+ with self .subTest (option = option ):
1336+ cmd = ["--fail-env-changed" , option , testname ]
1337+ output = self .run_tests (* cmd , exitcode = 3 )
1338+ self .check_executed_tests (output , [testname ],
1339+ env_changed = [testname ],
1340+ fail_env_changed = True )
1341+ self .assertRegex (output , regex )
1342+
13031343 def test_unicode_guard_env (self ):
13041344 guard = os .environ .get (setup .UNICODE_GUARD_ENV )
13051345 self .assertIsNotNone (guard , f"{ setup .UNICODE_GUARD_ENV } not set" )
0 commit comments