Skip to content

Commit 2840e06

Browse files
authored
Added cheaper implementation for no_other_refs() on Python 3.14 (#886)
1 parent 31ce0a5 commit 2840e06

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

tests/test_taskgroups.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -1619,26 +1619,16 @@ async def in_task_group(task_status: TaskStatus[None]) -> None:
16191619

16201620
if sys.version_info >= (3, 14):
16211621

1622-
async def no_other_refs() -> list[object]:
1623-
frame = sys._getframe(1)
1624-
coro = get_current_task().coro
1625-
1626-
async def get_coro_for_frame(*, task_status: TaskStatus[object]) -> None:
1627-
my_coro = coro
1628-
while my_coro.cr_frame is not frame:
1629-
my_coro = my_coro.cr_await
1630-
task_status.started(my_coro)
1631-
1632-
async with create_task_group() as tg:
1633-
return [await tg.start(get_coro_for_frame)]
1622+
def no_other_refs() -> list[object]:
1623+
return [sys._getframe(1).f_generator]
16341624

16351625
elif sys.version_info >= (3, 11):
16361626

1637-
async def no_other_refs() -> list[object]:
1627+
def no_other_refs() -> list[object]:
16381628
return []
16391629
else:
16401630

1641-
async def no_other_refs() -> list[object]:
1631+
def no_other_refs() -> list[object]:
16421632
return [sys._getframe(1)]
16431633

16441634

@@ -1670,7 +1660,7 @@ class _Done(Exception):
16701660
exc = e
16711661

16721662
assert exc is not None
1673-
assert gc.get_referrers(exc) == await no_other_refs()
1663+
assert gc.get_referrers(exc) == no_other_refs()
16741664

16751665
async def test_exception_refcycles_errors(self) -> None:
16761666
"""Test that TaskGroup deletes self._exceptions, and __aexit__ args"""
@@ -1687,7 +1677,7 @@ class _Done(Exception):
16871677
exc = excs.exceptions[0]
16881678

16891679
assert isinstance(exc, _Done)
1690-
assert gc.get_referrers(exc) == await no_other_refs()
1680+
assert gc.get_referrers(exc) == no_other_refs()
16911681

16921682
async def test_exception_refcycles_parent_task(self) -> None:
16931683
"""Test that TaskGroup's cancel_scope deletes self._host_task"""
@@ -1708,7 +1698,7 @@ async def coro_fn() -> None:
17081698
exc = excs.exceptions[0].exceptions[0]
17091699

17101700
assert isinstance(exc, _Done)
1711-
assert gc.get_referrers(exc) == await no_other_refs()
1701+
assert gc.get_referrers(exc) == no_other_refs()
17121702

17131703
async def test_exception_refcycles_propagate_cancellation_error(self) -> None:
17141704
"""Test that TaskGroup deletes cancelled_exc"""
@@ -1725,7 +1715,7 @@ async def test_exception_refcycles_propagate_cancellation_error(self) -> None:
17251715
raise
17261716

17271717
assert isinstance(exc, get_cancelled_exc_class())
1728-
assert gc.get_referrers(exc) == await no_other_refs()
1718+
assert gc.get_referrers(exc) == no_other_refs()
17291719

17301720
async def test_exception_refcycles_base_error(self) -> None:
17311721
"""
@@ -1748,7 +1738,7 @@ class MyKeyboardInterrupt(KeyboardInterrupt):
17481738
exc = excs.exceptions[0]
17491739

17501740
assert isinstance(exc, MyKeyboardInterrupt)
1751-
assert gc.get_referrers(exc) == await no_other_refs()
1741+
assert gc.get_referrers(exc) == no_other_refs()
17521742

17531743

17541744
class TestTaskStatusTyping:

0 commit comments

Comments
 (0)