From 29849ea9f006ca78d03480875aa553885c97d001 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sat, 26 Jun 2021 18:35:18 +1000 Subject: [PATCH 1/3] bpo-44515: handle non-refcounted GC in contextlib tests --- Lib/test/test_contextlib.py | 3 +++ Lib/test/test_contextlib_async.py | 15 +++++++++------ .../2021-06-26-18-37-36.bpo-44515.e9fO6f.rst | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Tests/2021-06-26-18-37-36.bpo-44515.e9fO6f.rst diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 453ef6c9f0832ff..ebc82325e6df691 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -9,6 +9,7 @@ from test import support from test.support import os_helper import weakref +import gc class TestAbstractContextManager(unittest.TestCase): @@ -228,6 +229,8 @@ class A: def woohoo(a, b): a = weakref.ref(a) b = weakref.ref(b) + # Allow test to work with a non-refcounted GC + gc.collect(); gc.collect(); gc.collect() self.assertIsNone(a()) self.assertIsNone(b()) yield diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index cbc82dfd8f8d07a..660489f9aa61e36 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -1,7 +1,7 @@ import asyncio from contextlib import ( asynccontextmanager, AbstractAsyncContextManager, - AsyncExitStack, nullcontext, aclosing) + AsyncExitStack, nullcontext, aclosing, contextmanager) import functools from test import support import unittest @@ -346,14 +346,17 @@ async def aclose(self): async def test_aclosing_bpo41229(self): state = [] - class Resource: - def __del__(self): + @contextmanager + def sync_resource(): + try: + yield + finally: state.append(1) async def agenfunc(): - r = Resource() - yield -1 - yield -2 + with sync_resource(): + yield -1 + yield -2 x = agenfunc() self.assertEqual(state, []) diff --git a/Misc/NEWS.d/next/Tests/2021-06-26-18-37-36.bpo-44515.e9fO6f.rst b/Misc/NEWS.d/next/Tests/2021-06-26-18-37-36.bpo-44515.e9fO6f.rst new file mode 100644 index 000000000000000..d2867b6e89f8726 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2021-06-26-18-37-36.bpo-44515.e9fO6f.rst @@ -0,0 +1,2 @@ +Adjust recently added contextlib tests to avoid assuming the use of a +refcounted GC From d92f9161bf62dc7a11032320f8b6fc1a2ac419be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 26 Jul 2021 18:15:14 +0200 Subject: [PATCH 2/3] Use test.support.gc_collect() --- Lib/test/test_contextlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index ebc82325e6df691..118ba7c73ed6a2f 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -230,7 +230,7 @@ def woohoo(a, b): a = weakref.ref(a) b = weakref.ref(b) # Allow test to work with a non-refcounted GC - gc.collect(); gc.collect(); gc.collect() + support.gc_collect() self.assertIsNone(a()) self.assertIsNone(b()) yield From e3a263ca002bd313a0c65571f2ac3620567365e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 26 Jul 2021 21:34:32 +0200 Subject: [PATCH 3/3] Remove unnecessary import --- Lib/test/test_contextlib.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 118ba7c73ed6a2f..848163f34cf9190 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -9,7 +9,6 @@ from test import support from test.support import os_helper import weakref -import gc class TestAbstractContextManager(unittest.TestCase):