summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_support.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-04-23 17:03:52 (GMT)
committerGitHub <noreply@github.com>2020-04-23 17:03:52 (GMT)
commitd663d34685e18588748569468c672763f4c73b3e (patch)
tree9d5dacf672a50135ef10efef37e3b51f20aac1ad /Lib/test/test_support.py
parent02e4484f19304a0a5f484f06a3fa441c6fb6073a (diff)
downloadcpython-d663d34685e18588748569468c672763f4c73b3e.zip
cpython-d663d34685e18588748569468c672763f4c73b3e.tar.gz
cpython-d663d34685e18588748569468c672763f4c73b3e.tar.bz2
bpo-39983: Add test.support.print_warning() (GH-19683)
Log "Warning -- ..." test warnings into sys.__stderr__ rather than sys.stderr, to ensure to display them even if sys.stderr is captured. test.libregrtest.utils.print_warning() now calls test.support.print_warning().
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r--Lib/test/test_support.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 99a4cad..dee1db7 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -430,8 +430,12 @@ class TestSupport(unittest.TestCase):
if time.monotonic() > deadline:
self.fail("timeout")
- with contextlib.redirect_stderr(stderr):
+ old_stderr = sys.__stderr__
+ try:
+ sys.__stderr__ = stderr
support.reap_children()
+ finally:
+ sys.__stderr__ = old_stderr
# Use environment_altered to check if reap_children() found
# the child process
@@ -629,6 +633,24 @@ class TestSupport(unittest.TestCase):
os.close(fd)
self.assertEqual(more - start, 1)
+ def check_print_warning(self, msg, expected):
+ stderr = io.StringIO()
+
+ old_stderr = sys.__stderr__
+ try:
+ sys.__stderr__ = stderr
+ support.print_warning(msg)
+ finally:
+ sys.__stderr__ = old_stderr
+
+ self.assertEqual(stderr.getvalue(), expected)
+
+ def test_print_warning(self):
+ self.check_print_warning("msg",
+ "Warning -- msg\n")
+ self.check_print_warning("a\nb",
+ 'Warning -- a\nWarning -- b\n')
+
# XXX -follows a list of untested API
# make_legacy_pyc
# is_resource_enabled