@@ -1416,6 +1416,7 @@ def run(x: object) -> object: ...
14161416
14171417[case testAsyncIntrospection]
14181418import asyncio
1419+ import gc
14191420import inspect
14201421import sys
14211422import weakref
@@ -1589,6 +1590,31 @@ def test_nested() -> None:
15891590 assert is_coroutine(nested_wrapped_async)
15901591 assert asyncio.run(nested_wrapped_async()) == 4
15911592
1593+ def test_async_function_wrapper_code_refcount() -> None:
1594+ code = getattr(identity_async, "__code__")
1595+ getrefcount = getattr(sys, "getrefcount")
1596+ # getrefcount sees the local code variable plus the wrapper-owned reference.
1597+ assert getrefcount(code) == 2, getrefcount(code)
1598+
1599+ def test_nested_async_function_wrapper_code_refcount() -> None:
1600+ def make_nested() -> Any:
1601+ async def nested_refcounted() -> int:
1602+ return 1
1603+
1604+ return nested_refcounted
1605+
1606+ getrefcount = getattr(sys, "getrefcount")
1607+ fn = make_nested()
1608+ code = getattr(fn, "__code__")
1609+ before = getrefcount(code)
1610+ assert asyncio.run(fn()) == 1
1611+
1612+ del fn
1613+ gc.collect()
1614+ after = getrefcount(code)
1615+ assert before == after + 1, (before, after)
1616+ assert after == 1, after
1617+
15921618[file asyncio/__init__.pyi]
15931619def run(x: object) -> object: ...
15941620
0 commit comments