@@ -367,10 +367,10 @@ def f(): """doc"""
367
367
def test_compile_top_level_await (self ):
368
368
"""Test whether code some top level await can be compiled.
369
369
370
- Make sure it compiles only with the PyCF_ALLOW_TOP_LEVEL_AWAIT flag set,
371
- and make sure the generated code object has the CO_COROUTINE flag set in
372
- order to execute it with `await eval(.....)` instead of exec, or via a
373
- FunctionType.
370
+ Make sure it compiles only with the PyCF_ALLOW_TOP_LEVEL_AWAIT flag
371
+ set, and make sure the generated code object has the CO_COROUTINE flag
372
+ set in order to execute it with `await eval(.....)` instead of exec,
373
+ or via a FunctionType.
374
374
"""
375
375
376
376
# helper function just to check we can run top=level async-for
@@ -379,35 +379,37 @@ async def arange(n):
379
379
yield i
380
380
381
381
modes = ('single' , 'exec' )
382
- code_samples = ['''a = await asyncio.sleep(0, result=1)''' ,
383
- '''async for i in arange(1):
384
- a = 1''' ,
385
- '''async with asyncio.Lock() as l:
386
- a = 1''' ]
382
+ code_samples = [
383
+ '''a = await asyncio.sleep(0, result=1)''' ,
384
+ '''async for i in arange(1):
385
+ a = 1''' ,
386
+ '''async with asyncio.Lock() as l:
387
+ a = 1'''
388
+ ]
387
389
policy = maybe_get_event_loop_policy ()
388
390
try :
389
- for mode , code_sample in product (modes ,code_samples ):
391
+ for mode , code_sample in product (modes , code_samples ):
390
392
source = dedent (code_sample )
391
- with self .assertRaises (SyntaxError , msg = f"{ source = } { mode = } " ):
392
- compile (source , '?' , mode )
393
+ with self .assertRaises (
394
+ SyntaxError , msg = f"source={ source } mode={ mode } " ):
395
+ compile (source , '?' , mode )
393
396
394
397
co = compile (source ,
395
398
'?' ,
396
399
mode ,
397
400
flags = ast .PyCF_ALLOW_TOP_LEVEL_AWAIT )
398
401
399
402
self .assertEqual (co .co_flags & CO_COROUTINE , CO_COROUTINE ,
400
- msg = f"{ source = } { mode = } " )
401
-
403
+ msg = f"source={ source } mode={ mode } " )
402
404
403
405
# test we can create and advance a function type
404
- globals_ = {'asyncio' : asyncio , 'a' :0 , 'arange' : arange }
406
+ globals_ = {'asyncio' : asyncio , 'a' : 0 , 'arange' : arange }
405
407
async_f = FunctionType (co , globals_ )
406
408
asyncio .run (async_f ())
407
409
self .assertEqual (globals_ ['a' ], 1 )
408
410
409
411
# test we can await-eval,
410
- globals_ = {'asyncio' : asyncio , 'a' :0 , 'arange' : arange }
412
+ globals_ = {'asyncio' : asyncio , 'a' : 0 , 'arange' : arange }
411
413
asyncio .run (eval (co , globals_ ))
412
414
self .assertEqual (globals_ ['a' ], 1 )
413
415
finally :
@@ -416,7 +418,8 @@ async def arange(n):
416
418
def test_compile_async_generator (self ):
417
419
"""
418
420
With the PyCF_ALLOW_TOP_LEVEL_AWAIT flag added in 3.8, we want to
419
- make sure AsyncGenerators are still properly not marked with CO_COROUTINE
421
+ make sure AsyncGenerators are still properly not marked with the
422
+ CO_COROUTINE flag.
420
423
"""
421
424
code = dedent ("""async def ticker():
422
425
for i in range(10):
@@ -428,7 +431,6 @@ def test_compile_async_generator(self):
428
431
exec (co , glob )
429
432
self .assertEqual (type (glob ['ticker' ]()), AsyncGeneratorType )
430
433
431
-
432
434
def test_delattr (self ):
433
435
sys .spam = 1
434
436
delattr (sys , 'spam' )
0 commit comments