MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_compile_top_level_await

Method test_compile_top_level_await

Lib/test/test_builtin.py:469–551  ·  view source on GitHub ↗

Test whether code with top level await can be compiled. Make sure it compiles only with the PyCF_ALLOW_TOP_LEVEL_AWAIT flag set, and make sure the generated code object has the CO_COROUTINE flag set in order to execute it with `await eval(.....)` instead of exec, or

(self)

Source from the content-addressed store, hash-verified

467
468 @unittest.expectedFailure # TODO: RUSTPYTHON
469 def test_compile_top_level_await(self):
470 """Test whether code with top level await can be compiled.
471
472 Make sure it compiles only with the PyCF_ALLOW_TOP_LEVEL_AWAIT flag
473 set, and make sure the generated code object has the CO_COROUTINE flag
474 set in order to execute it with `await eval(.....)` instead of exec,
475 or via a FunctionType.
476 """
477
478 # helper function just to check we can run top=level async-for
479 async def arange(n):
480 for i in range(n):
481 yield i
482
483 class Lock:
484 async def __aenter__(self):
485 return self
486
487 async def __aexit__(self, *exc_info):
488 pass
489
490 async def sleep(delay, result=None):
491 assert delay == 0
492 await async_yield(None)
493 return result
494
495 modes = ('single', 'exec')
496 optimizations = (-1, 0, 1, 2)
497 code_samples = [
498 '''a = await sleep(0, result=1)''',
499 '''async for i in arange(1):
500 a = 1''',
501 '''async with Lock() as l:
502 a = 1''',
503 '''a = [x async for x in arange(2)][1]''',
504 '''a = 1 in {x async for x in arange(2)}''',
505 '''a = {x:1 async for x in arange(1)}[0]''',
506 '''a = [x async for x in arange(2) async for x in arange(2)][1]''',
507 '''a = [x async for x in (x async for x in arange(5))][1]''',
508 '''a, = [1 for x in {x async for x in arange(1)}]''',
509 '''a = [await sleep(0, x) async for x in arange(2)][1]''',
510 # gh-121637: Make sure we correctly handle the case where the
511 # async code is optimized away
512 '''assert not await sleep(0); a = 1''',
513 '''assert [x async for x in arange(1)]; a = 1''',
514 '''assert {x async for x in arange(1)}; a = 1''',
515 '''assert {x: x async for x in arange(1)}; a = 1''',
516 '''
517 if (a := 1) and __debug__:
518 async with Lock() as l:
519 pass
520 ''',
521 '''
522 if (a := 1) and __debug__:
523 async for x in arange(2):
524 pass
525 ''',
526 ]

Callers

nothing calls this directly

Calls 8

dedentFunction · 0.90
run_yielding_async_fnFunction · 0.90
FunctionTypeFunction · 0.90
subTestMethod · 0.80
compileFunction · 0.50
evalFunction · 0.50
assertRaisesMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected