11# Python test set -- built-in functions
22
33import ast
4- import asyncio
54import builtins
65import collections
76import contextlib
3130from types import AsyncGeneratorType , FunctionType , CellType
3231from operator import neg
3332from 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
3535from test .support .import_helper import import_module
3636from test .support .os_helper import (EnvironmentVarGuard , TESTFN , unlink )
3737from test .support .script_helper import assert_python_ok
@@ -414,10 +414,6 @@ def test_compile_top_level_await_no_coro(self):
414414 msg = f"source={ source } mode={ mode } " )
415415
416416
417- @unittest .skipIf (
418- support .is_emscripten or support .is_wasi ,
419- "socket.accept is broken"
420- )
421417 def test_compile_top_level_await (self ):
422418 """Test whether code with top level await can be compiled.
423419
@@ -432,30 +428,42 @@ async def arange(n):
432428 for i in range (n ):
433429 yield i
434430
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+
435443 modes = ('single' , 'exec' )
436444 optimizations = (- 1 , 0 , 1 , 2 )
437445 code_samples = [
438- '''a = await asyncio. sleep(0, result=1)''' ,
446+ '''a = await sleep(0, result=1)''' ,
439447 '''async for i in arange(1):
440448 a = 1''' ,
441- '''async with asyncio. Lock() as l:
449+ '''async with Lock() as l:
442450 a = 1''' ,
443451 '''a = [x async for x in arange(2)][1]''' ,
444452 '''a = 1 in {x async for x in arange(2)}''' ,
445453 '''a = {x:1 async for x in arange(1)}[0]''' ,
446454 '''a = [x async for x in arange(2) async for x in arange(2)][1]''' ,
447455 '''a = [x async for x in (x async for x in arange(5))][1]''' ,
448456 '''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]''' ,
450458 # gh-121637: Make sure we correctly handle the case where the
451459 # async code is optimized away
452- '''assert not await asyncio. sleep(0); a = 1''' ,
460+ '''assert not await sleep(0); a = 1''' ,
453461 '''assert [x async for x in arange(1)]; a = 1''' ,
454462 '''assert {x async for x in arange(1)}; a = 1''' ,
455463 '''assert {x: x async for x in arange(1)}; a = 1''' ,
456464 '''
457465 if (a := 1) and __debug__:
458- async with asyncio. Lock() as l:
466+ async with Lock() as l:
459467 pass
460468 ''' ,
461469 '''
@@ -464,43 +472,45 @@ async def arange(n):
464472 pass
465473 ''' ,
466474 ]
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 )
497500
498501 def test_compile_top_level_await_invalid_cases (self ):
499502 # helper function just to check we can run top=level async-for
500503 async def arange (n ):
501504 for i in range (n ):
502505 yield i
503506
507+ class Lock :
508+ async def __aenter__ (self ):
509+ return self
510+
511+ async def __aexit__ (self , * exc_info ):
512+ pass
513+
504514 modes = ('single' , 'exec' )
505515 code_samples = [
506516 '''def f(): await arange(10)\n ''' ,
@@ -511,27 +521,22 @@ async def arange(n):
511521 a = 1
512522 ''' ,
513523 '''def f():
514- async with asyncio. Lock() as l:
524+ async with Lock() as l:
515525 a = 1
516526 '''
517527 ]
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 )
534533
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 )
535540
536541 def test_compile_async_generator (self ):
537542 """
@@ -542,7 +547,7 @@ def test_compile_async_generator(self):
542547 code = dedent ("""async def ticker():
543548 for i in range(10):
544549 yield i
545- await asyncio. sleep(0)""" )
550+ await sleep(0)""" )
546551
547552 co = compile (code , '?' , 'exec' , flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT )
548553 glob = {}
0 commit comments