@@ -1235,10 +1235,12 @@ def test_sleep(self):
12351235 re .compile ('%s timed out' % testname , re .MULTILINE ))
12361236
12371237 def test_unraisable_exc (self ):
1238- # --fail-env-changed must catch unraisable exception
1238+ # --fail-env-changed must catch unraisable exception.
1239+ # The exceptioin must be displayed even if sys.stderr is redirected.
12391240 code = textwrap .dedent (r"""
12401241 import unittest
12411242 import weakref
1243+ from test.support import captured_stderr
12421244
12431245 class MyObject:
12441246 pass
@@ -1250,9 +1252,11 @@ class Tests(unittest.TestCase):
12501252 def test_unraisable_exc(self):
12511253 obj = MyObject()
12521254 ref = weakref.ref(obj, weakref_callback)
1253- # call weakref_callback() which logs
1254- # an unraisable exception
1255- obj = None
1255+ with captured_stderr() as stderr:
1256+ # call weakref_callback() which logs
1257+ # an unraisable exception
1258+ obj = None
1259+ self.assertEqual(stderr.getvalue(), '')
12561260 """ )
12571261 testname = self .create_test (code = code )
12581262
@@ -1261,6 +1265,7 @@ def test_unraisable_exc(self):
12611265 env_changed = [testname ],
12621266 fail_env_changed = True )
12631267 self .assertIn ("Warning -- Unraisable exception" , output )
1268+ self .assertIn ("Exception: weakref callback bug" , output )
12641269
12651270 def test_cleanup (self ):
12661271 dirname = os .path .join (self .tmptestdir , "test_python_123" )
0 commit comments