From 89abf3bf3c919320f7b0f82564c6c5d1d1b7090c Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 22 Dec 2024 12:46:02 +0000 Subject: [PATCH 1/3] gh-127949: fix resource warnings in `test_tasks.py` (GH-128172) (cherry picked from commit b66a4ad9fc32b63da2ba10db24cbc8f4e29f781a) Co-authored-by: Thomas Grainger --- Lib/test/test_asyncio/test_tasks.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 792ab4fbede0a2..4ddf1fdf3fada7 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2499,17 +2499,17 @@ def __str__(self): initial_refcount = sys.getrefcount(obj) coro = coroutine_function() - loop = asyncio.new_event_loop() - task = asyncio.Task.__new__(asyncio.Task) + with contextlib.closing(asyncio.EventLoop()) as loop: + task = asyncio.Task.__new__(asyncio.Task) - for _ in range(5): - with self.assertRaisesRegex(RuntimeError, 'break'): - task.__init__(coro, loop=loop, context=obj, name=Break()) + for _ in range(5): + with self.assertRaisesRegex(RuntimeError, 'break'): + task.__init__(coro, loop=loop, context=obj, name=Break()) - coro.close() - del task + coro.close() + del task - self.assertEqual(sys.getrefcount(obj), initial_refcount) + self.assertEqual(sys.getrefcount(obj), initial_refcount) def add_subclass_tests(cls): From 50ab6261451784c76b284aa52099adc31da878c5 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 27 Mar 2025 21:37:14 +0000 Subject: [PATCH 2/3] Update Lib/test/test_asyncio/test_tasks.py --- Lib/test/test_asyncio/test_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 4ddf1fdf3fada7..daeb4bdbb24346 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2499,7 +2499,7 @@ def __str__(self): initial_refcount = sys.getrefcount(obj) coro = coroutine_function() - with contextlib.closing(asyncio.EventLoop()) as loop: + with contextlib.closing(asyncio.new_event_loop()) as loop: task = asyncio.Task.__new__(asyncio.Task) for _ in range(5): From 1ecc4ca920e74963a6f4671ca6798575e167ae6f Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 28 Mar 2025 19:38:05 +0530 Subject: [PATCH 3/3] import contextlib --- Lib/test/test_asyncio/test_tasks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index daeb4bdbb24346..428bd1de16fda8 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1,6 +1,7 @@ """Tests for tasks.py.""" import collections +import contextlib import contextvars import gc import io