From e4d300e07c33a9a77549c62d8687d8fe130c53d5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 22 May 2019 23:44:02 +0200 Subject: bpo-36829: Add test.support.catch_unraisable_exception() (GH-13490) * Copy test_exceptions.test_unraisable() to test_sys.UnraisableHookTest(). * Use catch_unraisable_exception() in test_coroutines, test_exceptions, test_generators. --- Lib/test/test_coroutines.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'Lib/test/test_coroutines.py') diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 8443e658a6..036f13fa50 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -2342,12 +2342,19 @@ class OriginTrackingTest(unittest.TestCase): orig_wuc = warnings._warn_unawaited_coroutine try: warnings._warn_unawaited_coroutine = lambda coro: 1/0 - with support.captured_stderr() as stream: - corofn() + with support.catch_unraisable_exception() as cm, \ + support.captured_stderr() as stream: + # only store repr() to avoid keeping the coroutine alive + coro = corofn() + coro_repr = repr(coro) + + # clear reference to the coroutine without awaiting for it + del coro support.gc_collect() - self.assertIn("Exception ignored in", stream.getvalue()) - self.assertIn("ZeroDivisionError", stream.getvalue()) - self.assertIn("was never awaited", stream.getvalue()) + + self.assertEqual(repr(cm.unraisable.object), coro_repr) + self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError) + self.assertIn("was never awaited", stream.getvalue()) del warnings._warn_unawaited_coroutine with support.captured_stderr() as stream: -- cgit v1.2.1