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.
This commit is contained in:
parent
4660597b51
commit
701b63894f
|
@ -72,7 +72,12 @@ def regrtest_unraisable_hook(unraisable):
|
||||||
global orig_unraisablehook
|
global orig_unraisablehook
|
||||||
support.environment_altered = True
|
support.environment_altered = True
|
||||||
print_warning("Unraisable exception")
|
print_warning("Unraisable exception")
|
||||||
|
old_stderr = sys.stderr
|
||||||
|
try:
|
||||||
|
sys.stderr = sys.__stderr__
|
||||||
orig_unraisablehook(unraisable)
|
orig_unraisablehook(unraisable)
|
||||||
|
finally:
|
||||||
|
sys.stderr = old_stderr
|
||||||
|
|
||||||
|
|
||||||
def setup_unraisable_hook():
|
def setup_unraisable_hook():
|
||||||
|
|
|
@ -1235,10 +1235,12 @@ class ArgsTestCase(BaseTestCase):
|
||||||
re.compile('%s timed out' % testname, re.MULTILINE))
|
re.compile('%s timed out' % testname, re.MULTILINE))
|
||||||
|
|
||||||
def test_unraisable_exc(self):
|
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"""
|
code = textwrap.dedent(r"""
|
||||||
import unittest
|
import unittest
|
||||||
import weakref
|
import weakref
|
||||||
|
from test.support import captured_stderr
|
||||||
|
|
||||||
class MyObject:
|
class MyObject:
|
||||||
pass
|
pass
|
||||||
|
@ -1250,9 +1252,11 @@ class ArgsTestCase(BaseTestCase):
|
||||||
def test_unraisable_exc(self):
|
def test_unraisable_exc(self):
|
||||||
obj = MyObject()
|
obj = MyObject()
|
||||||
ref = weakref.ref(obj, weakref_callback)
|
ref = weakref.ref(obj, weakref_callback)
|
||||||
|
with captured_stderr() as stderr:
|
||||||
# call weakref_callback() which logs
|
# call weakref_callback() which logs
|
||||||
# an unraisable exception
|
# an unraisable exception
|
||||||
obj = None
|
obj = None
|
||||||
|
self.assertEqual(stderr.getvalue(), '')
|
||||||
""")
|
""")
|
||||||
testname = self.create_test(code=code)
|
testname = self.create_test(code=code)
|
||||||
|
|
||||||
|
@ -1261,6 +1265,7 @@ class ArgsTestCase(BaseTestCase):
|
||||||
env_changed=[testname],
|
env_changed=[testname],
|
||||||
fail_env_changed=True)
|
fail_env_changed=True)
|
||||||
self.assertIn("Warning -- Unraisable exception", output)
|
self.assertIn("Warning -- Unraisable exception", output)
|
||||||
|
self.assertIn("Exception: weakref callback bug", output)
|
||||||
|
|
||||||
def test_cleanup(self):
|
def test_cleanup(self):
|
||||||
dirname = os.path.join(self.tmptestdir, "test_python_123")
|
dirname = os.path.join(self.tmptestdir, "test_python_123")
|
||||||
|
|
Loading…
Reference in New Issue