Skip to content

[3.9] bpo-38912: regrtest logs unraisable exception into sys.__stderr__ (GH-21718) #21827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/test/libregrtest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
13 changes: 9 additions & 4 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,10 +1234,12 @@ def test_sleep(self):
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
Expand All @@ -1249,9 +1251,11 @@ class Tests(unittest.TestCase):
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)

Expand All @@ -1260,6 +1264,7 @@ def test_unraisable_exc(self):
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")
Expand Down