1
1
# Python test set -- built-in functions
2
2
3
3
import ast
4
- import asyncio
5
4
import builtins
6
5
import collections
7
6
import contextlib
31
30
from types import AsyncGeneratorType , FunctionType , CellType
32
31
from operator import neg
33
32
from test import support
34
- from test .support import (cpython_only , swap_attr , maybe_get_event_loop_policy )
33
+ from test .support import cpython_only , swap_attr
34
+ from test .support import async_yield , run_yielding_async_fn
35
35
from test .support .import_helper import import_module
36
36
from test .support .os_helper import (EnvironmentVarGuard , TESTFN , unlink )
37
37
from test .support .script_helper import assert_python_ok
@@ -414,10 +414,6 @@ def test_compile_top_level_await_no_coro(self):
414
414
msg = f"source={ source } mode={ mode } " )
415
415
416
416
417
- @unittest .skipIf (
418
- support .is_emscripten or support .is_wasi ,
419
- "socket.accept is broken"
420
- )
421
417
def test_compile_top_level_await (self ):
422
418
"""Test whether code with top level await can be compiled.
423
419
@@ -432,30 +428,42 @@ async def arange(n):
432
428
for i in range (n ):
433
429
yield i
434
430
431
+ class Lock :
432
+ async def __aenter__ (self ):
433
+ return self
434
+
435
+ async def __aexit__ (self , * exc_info ):
436
+ pass
437
+
438
+ async def sleep (delay , result = None ):
439
+ assert delay == 0
440
+ await async_yield (None )
441
+ return result
442
+
435
443
modes = ('single' , 'exec' )
436
444
optimizations = (- 1 , 0 , 1 , 2 )
437
445
code_samples = [
438
- '''a = await asyncio. sleep(0, result=1)''' ,
446
+ '''a = await sleep(0, result=1)''' ,
439
447
'''async for i in arange(1):
440
448
a = 1''' ,
441
- '''async with asyncio. Lock() as l:
449
+ '''async with Lock() as l:
442
450
a = 1''' ,
443
451
'''a = [x async for x in arange(2)][1]''' ,
444
452
'''a = 1 in {x async for x in arange(2)}''' ,
445
453
'''a = {x:1 async for x in arange(1)}[0]''' ,
446
454
'''a = [x async for x in arange(2) async for x in arange(2)][1]''' ,
447
455
'''a = [x async for x in (x async for x in arange(5))][1]''' ,
448
456
'''a, = [1 for x in {x async for x in arange(1)}]''' ,
449
- '''a = [await asyncio. sleep(0, x) async for x in arange(2)][1]''' ,
457
+ '''a = [await sleep(0, x) async for x in arange(2)][1]''' ,
450
458
# gh-121637: Make sure we correctly handle the case where the
451
459
# async code is optimized away
452
- '''assert not await asyncio. sleep(0); a = 1''' ,
460
+ '''assert not await sleep(0); a = 1''' ,
453
461
'''assert [x async for x in arange(1)]; a = 1''' ,
454
462
'''assert {x async for x in arange(1)}; a = 1''' ,
455
463
'''assert {x: x async for x in arange(1)}; a = 1''' ,
456
464
'''
457
465
if (a := 1) and __debug__:
458
- async with asyncio. Lock() as l:
466
+ async with Lock() as l:
459
467
pass
460
468
''' ,
461
469
'''
@@ -464,43 +472,45 @@ async def arange(n):
464
472
pass
465
473
''' ,
466
474
]
467
- policy = maybe_get_event_loop_policy ()
468
- try :
469
- for mode , code_sample , optimize in product (modes , code_samples , optimizations ):
470
- with self .subTest (mode = mode , code_sample = code_sample , optimize = optimize ):
471
- source = dedent (code_sample )
472
- with self .assertRaises (
473
- SyntaxError , msg = f"source={ source } mode={ mode } " ):
474
- compile (source , '?' , mode , optimize = optimize )
475
-
476
- co = compile (source ,
477
- '?' ,
478
- mode ,
479
- flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT ,
480
- optimize = optimize )
481
-
482
- self .assertEqual (co .co_flags & CO_COROUTINE , CO_COROUTINE ,
483
- msg = f"source={ source } mode={ mode } " )
484
-
485
- # test we can create and advance a function type
486
- globals_ = {'asyncio' : asyncio , 'a' : 0 , 'arange' : arange }
487
- async_f = FunctionType (co , globals_ )
488
- asyncio .run (async_f ())
489
- self .assertEqual (globals_ ['a' ], 1 )
490
-
491
- # test we can await-eval,
492
- globals_ = {'asyncio' : asyncio , 'a' : 0 , 'arange' : arange }
493
- asyncio .run (eval (co , globals_ ))
494
- self .assertEqual (globals_ ['a' ], 1 )
495
- finally :
496
- asyncio ._set_event_loop_policy (policy )
475
+ for mode , code_sample , optimize in product (modes , code_samples , optimizations ):
476
+ with self .subTest (mode = mode , code_sample = code_sample , optimize = optimize ):
477
+ source = dedent (code_sample )
478
+ with self .assertRaises (
479
+ SyntaxError , msg = f"source={ source } mode={ mode } " ):
480
+ compile (source , '?' , mode , optimize = optimize )
481
+
482
+ co = compile (source ,
483
+ '?' ,
484
+ mode ,
485
+ flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT ,
486
+ optimize = optimize )
487
+
488
+ self .assertEqual (co .co_flags & CO_COROUTINE , CO_COROUTINE ,
489
+ msg = f"source={ source } mode={ mode } " )
490
+
491
+ # test we can create and advance a function type
492
+ globals_ = {'Lock' : Lock , 'a' : 0 , 'arange' : arange , 'sleep' : sleep }
493
+ run_yielding_async_fn (FunctionType (co , globals_ ))
494
+ self .assertEqual (globals_ ['a' ], 1 )
495
+
496
+ # test we can await-eval,
497
+ globals_ = {'Lock' : Lock , 'a' : 0 , 'arange' : arange , 'sleep' : sleep }
498
+ run_yielding_async_fn (lambda : eval (co , globals_ ))
499
+ self .assertEqual (globals_ ['a' ], 1 )
497
500
498
501
def test_compile_top_level_await_invalid_cases (self ):
499
502
# helper function just to check we can run top=level async-for
500
503
async def arange (n ):
501
504
for i in range (n ):
502
505
yield i
503
506
507
+ class Lock :
508
+ async def __aenter__ (self ):
509
+ return self
510
+
511
+ async def __aexit__ (self , * exc_info ):
512
+ pass
513
+
504
514
modes = ('single' , 'exec' )
505
515
code_samples = [
506
516
'''def f(): await arange(10)\n ''' ,
@@ -511,27 +521,22 @@ async def arange(n):
511
521
a = 1
512
522
''' ,
513
523
'''def f():
514
- async with asyncio. Lock() as l:
524
+ async with Lock() as l:
515
525
a = 1
516
526
'''
517
527
]
518
- policy = maybe_get_event_loop_policy ()
519
- try :
520
- for mode , code_sample in product (modes , code_samples ):
521
- source = dedent (code_sample )
522
- with self .assertRaises (
523
- SyntaxError , msg = f"source={ source } mode={ mode } " ):
524
- compile (source , '?' , mode )
525
-
526
- with self .assertRaises (
527
- SyntaxError , msg = f"source={ source } mode={ mode } " ):
528
- co = compile (source ,
529
- '?' ,
530
- mode ,
531
- flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT )
532
- finally :
533
- asyncio ._set_event_loop_policy (policy )
528
+ for mode , code_sample in product (modes , code_samples ):
529
+ source = dedent (code_sample )
530
+ with self .assertRaises (
531
+ SyntaxError , msg = f"source={ source } mode={ mode } " ):
532
+ compile (source , '?' , mode )
534
533
534
+ with self .assertRaises (
535
+ SyntaxError , msg = f"source={ source } mode={ mode } " ):
536
+ co = compile (source ,
537
+ '?' ,
538
+ mode ,
539
+ flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT )
535
540
536
541
def test_compile_async_generator (self ):
537
542
"""
@@ -542,7 +547,7 @@ def test_compile_async_generator(self):
542
547
code = dedent ("""async def ticker():
543
548
for i in range(10):
544
549
yield i
545
- await asyncio. sleep(0)""" )
550
+ await sleep(0)""" )
546
551
547
552
co = compile (code , '?' , 'exec' , flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT )
548
553
glob = {}
0 commit comments