Skip to content

Commit

Permalink
[3.11] gh-103186: Suppress RuntimeWarning about unclosed async iterat…
Browse files Browse the repository at this point in the history
…or in test_sys_settrace (GH-109075) (GH-109086)

(cherry picked from commit d485551)
  • Loading branch information
serhiy-storchaka authored Sep 7, 2023
1 parent 12f40a4 commit 49cc2e7
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ async def asynciter(iterable):
for x in iterable:
yield x

def clean_asynciter(test):
@wraps(test)
async def wrapper(*args, **kwargs):
cleanups = []
def wrapped_asynciter(iterable):
it = asynciter(iterable)
cleanups.append(it.aclose)
return it
try:
return await test(*args, **kwargs, asynciter=wrapped_asynciter)
finally:
while cleanups:
await cleanups.pop()()
return wrapper

# A very basic example. If this fails, we're in deep trouble.
def basic():
Expand Down Expand Up @@ -1868,7 +1882,11 @@ def compare_jump_output(self, expected, received):

def run_test(self, func, jumpFrom, jumpTo, expected, error=None,
event='line', decorated=False):
tracer = JumpTracer(func, jumpFrom, jumpTo, event, decorated)
wrapped = func
while hasattr(wrapped, '__wrapped__'):
wrapped = wrapped.__wrapped__

tracer = JumpTracer(wrapped, jumpFrom, jumpTo, event, decorated)
sys.settrace(tracer.trace)
output = []
if error is None:
Expand All @@ -1881,7 +1899,11 @@ def run_test(self, func, jumpFrom, jumpTo, expected, error=None,

def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None,
event='line', decorated=False):
tracer = JumpTracer(func, jumpFrom, jumpTo, event, decorated)
wrapped = func
while hasattr(wrapped, '__wrapped__'):
wrapped = wrapped.__wrapped__

tracer = JumpTracer(wrapped, jumpFrom, jumpTo, event, decorated)
sys.settrace(tracer.trace)
output = []
if error is None:
Expand Down Expand Up @@ -1949,15 +1971,17 @@ def test_jump_out_of_block_backwards(output):
output.append(7)

@async_jump_test(4, 5, [3, 5])
async def test_jump_out_of_async_for_block_forwards(output):
@clean_asynciter
async def test_jump_out_of_async_for_block_forwards(output, asynciter):
for i in [1]:
async for i in asynciter([1, 2]):
output.append(3)
output.append(4)
output.append(5)

@async_jump_test(5, 2, [2, 4, 2, 4, 5, 6])
async def test_jump_out_of_async_for_block_backwards(output):
@clean_asynciter
async def test_jump_out_of_async_for_block_backwards(output, asynciter):
for i in [1]:
output.append(2)
async for i in asynciter([1]):
Expand Down

0 comments on commit 49cc2e7

Please sign in to comment.