8000 bpo-45410: regrtest replaces print_warning.orig_stderr (GH-28926) · python/cpython@676201a · GitHub
[go: up one dir, main page]

Skip to content

Commit 676201a

Browse files
authored
bpo-45410: regrtest replaces print_warning.orig_stderr (GH-28926)
When running Python tests with -W, runtest() now replaces support.print_warning.orig_stderr to preserve the messages order. Add an unit test.
1 parent 713bb19 commit 676201a

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Lib/test/libregrtest/runtest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,26 @@ def _runtest(ns: Namespace, test_name: str) -> TestResult:
196196
stream = io.StringIO()
197197
orig_stdout = sys.stdout
198198
orig_stderr = sys.stderr
199+
print_warning = support.print_warning
200+
orig_print_warnings_stderr = print_warning.orig_stderr
201+
199202
output = None
200203
try:
201204
sys.stdout = stream
202205
sys.stderr = stream
206+
# print_warning() writes into the temporary stream to preserve
207+
# messages order. If support.environment_altered becomes true,
208+
# warnings will be written to sys.stderr below.
209+
print_warning.orig_stderr = stream
210+
203211
result = _runtest_inner(ns, test_name,
204212
display_failure=False)
205213
if not isinstance(result, Passed):
206214
output = stream.getvalue()
207215
finally:
208216
sys.stdout = orig_stdout
209217
sys.stderr = orig_stderr
218+
print_warning.orig_stderr = orig_print_warnings_stderr
210219

211220
if output is not None:
212221
sys.stderr.write(output)

Lib/test/libregrtest/save_env.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
320320
support.environment_altered = True
321321
restore(original)
322322
if not self.quiet and not self.pgo:
323-
print_warning(f"{name} was modified by {self.testname}")
324-
print(f" Before: {original}\n After: {current} ",
325-
file=sys.stderr, flush=True)
323+
print_warning(
324+
f"{name} was modified by {self.testname}\n"
325+
f" Before: {original}\n"
326+
f" After: {current} ")
326327
return False

Lib/test/test_regrtest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
0