diff options
author | Victor Stinner <vstinner@python.org> | 2020-08-03 20:51:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-03 20:51:23 (GMT) |
commit | 701b63894fdb75b12865b9be6261ce4913da76f5 (patch) | |
tree | 89db278afe0bb0be142c6509e18915c0b8fc1db2 | |
parent | 4660597b51b3d14ce6269d0ed865ab7e22c6ae1f (diff) | |
download | cpython-701b63894fdb75b12865b9be6261ce4913da76f5.zip cpython-701b63894fdb75b12865b9be6261ce4913da76f5.tar.gz cpython-701b63894fdb75b12865b9be6261ce4913da76f5.tar.bz2 |
bpo-38912: regrtest logs unraisable exception into sys.__stderr__ (GH-21718)
regrtest_unraisable_hook() temporarily replaces sys.stderr with
sys.__stderr__ to help to display errors when a test captures stderr.
-rw-r--r-- | Lib/test/libregrtest/utils.py | 7 | ||||
-rw-r--r-- | Lib/test/test_regrtest.py | 13 |
2 files changed, 15 insertions, 5 deletions
diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 0368694..71f538f 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -72,7 +72,12 @@ def regrtest_unraisable_hook(unraisable): global orig_unraisablehook support.environment_altered = True print_warning("Unraisable exception") - orig_unraisablehook(unraisable) + old_stderr = sys.stderr + try: + sys.stderr = sys.__stderr__ + orig_unraisablehook(unraisable) + finally: + sys.stderr = old_stderr def setup_unraisable_hook(): diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py index 39af0d9..38321e0 100644 --- a/Lib/test/test_regrtest.py +++ b/Lib/test/test_regrtest.py @@ -1235,10 +1235,12 @@ class ArgsTestCase(BaseTestCase): re.compile('%s timed out' % testname, re.MULTILINE)) def test_unraisable_exc(self): - # --fail-env-changed must catch unraisable exception + # --fail-env-changed must catch unraisable exception. + # The exceptioin must be displayed even if sys.stderr is redirected. code = textwrap.dedent(r""" import unittest import weakref + from test.support import captured_stderr class MyObject: pass @@ -1250,9 +1252,11 @@ class ArgsTestCase(BaseTestCase): def test_unraisable_exc(self): obj = MyObject() ref = weakref.ref(obj, weakref_callback) - # call weakref_callback() which logs - # an unraisable exception - obj = None + with captured_stderr() as stderr: + # call weakref_callback() which logs + # an unraisable exception + obj = None + self.assertEqual(stderr.getvalue(), '') """) testname = self.create_test(code=code) @@ -1261,6 +1265,7 @@ class ArgsTestCase(BaseTestCase): env_changed=[testname], fail_env_changed=True) self.assertIn("Warning -- Unraisable exception", output) + self.assertIn("Exception: weakref callback bug", output) def test_cleanup(self): dirname = os.path.join(self.tmptestdir, "test_python_123") |