Skip to content

Commit 0ea5e0d

Browse files
bpo-44515: handle non-refcounted GC in contextlib tests (GH-26910) (GH-27379)
Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit a2c45e5) Co-authored-by: Nick Coghlan <ncoghlan@gmail.com>
1 parent 3dc88ff commit 0ea5e0d

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

Lib/test/test_contextlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ class A:
231231
def woohoo(a, b):
232232
a = weakref.ref(a)
233233
b = weakref.ref(b)
234+
# Allow test to work with a non-refcounted GC
235+
support.gc_collect()
234236
self.assertIsNone(a())
235237
self.assertIsNone(b())
236238
yield

Lib/test/test_contextlib_async.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
from contextlib import (
33
asynccontextmanager, AbstractAsyncContextManager,
4-
AsyncExitStack, nullcontext, aclosing)
4+
AsyncExitStack, nullcontext, aclosing, contextmanager)
55
import functools
66
from test import support
77
import unittest
@@ -357,14 +357,17 @@ async def aclose(self):
357357
async def test_aclosing_bpo41229(self):
358358
state = []
359359

360-
class Resource:
361-
def __del__(self):
360+
@contextmanager
361+
def sync_resource():
362+
try:
363+
yield
364+
finally:
362365
state.append(1)
363366

364367
async def agenfunc():
365-
r = Resource()
366-
yield -1
367-
yield -2
368+
with sync_resource():
369+
yield -1
370+
yield -2
368371

369372
x = agenfunc()
370373
self.assertEqual(state, [])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Adjust recently added contextlib tests to avoid assuming the use of a
2+
refcounted GC

0 commit comments

Comments
 (0)