@@ -2321,6 +2321,138 @@ async def func(a=None):
23212321 {'a' : None , 'gencoro' : gencoro , 'b' : 'spam' })
23222322
23232323
2324+ class TestGetAsyncGenState (unittest .TestCase ):
2325+
2326+ def setUp (self ):
2327+ async def number_asyncgen ():
2328+ for number in range (5 ):
2329+ yield number
2330+ self .asyncgen = number_asyncgen ()
2331+
2332+ def tearDown (self ):
2333+ try :
2334+ self .asyncgen .aclose ().send (None )
2335+ except StopIteration :
2336+ pass
2337+
2338+ def _asyncgenstate (self ):
2339+ return inspect .getasyncgenstate (self .asyncgen )
2340+
2341+ def test_created (self ):
2342+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_CREATED )
2343+
2344+ def test_suspended (self ):
2345+ try :
2346+ next (self .asyncgen .__anext__ ())
2347+ except StopIteration as exc :
2348+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_SUSPENDED )
2349+ self .assertEqual (exc .args , (0 ,))
2350+
2351+ def test_closed_after_exhaustion (self ):
2352+ while True :
2353+ try :
2354+ next (self .asyncgen .__anext__ ())
2355+ except StopAsyncIteration :
2356+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_CLOSED )
2357+ break
2358+ except StopIteration as exc :
2359+ if exc .args is None :
2360+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_CLOSED )
2361+ break
2362+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_CLOSED )
2363+
2364+ def test_closed_after_immediate_exception (self ):
2365+ with self .assertRaises (RuntimeError ):
2366+ self .asyncgen .athrow (RuntimeError ).send (None )
2367+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_CLOSED )
2368+
2369+ def test_running (self ):
2370+ async def running_check_asyncgen ():
2371+ for number in range (5 ):
2372+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_RUNNING )
2373+ yield number
2374+ self .assertEqual (self ._asyncgenstate (), inspect .AGEN_RUNNING )
2375+ self .asyncgen = running_check_asyncgen ()
2376+ # Running up to the first yield
2377+ try :
2378+ next (self .asyncgen .__anext__ ())
2379+ except StopIteration :
2380+ pass
2381+ # Running after the first yield
2382+ try :
2383+ next (self .asyncgen .__anext__ ())
2384+ except StopIteration :
2385+ pass
2386+
2387+ def test_easy_debugging (self ):
2388+ # repr() and str() of a asyncgen state should contain the state name
2389+ names = 'AGEN_CREATED AGEN_RUNNING AGEN_SUSPENDED AGEN_CLOSED' .split ()
2390+ for name in names :
2391+ state = getattr (inspect , name )
2392+ self .assertIn (name , repr (state ))
2393+ self .assertIn (name , str (state ))
2394+
2395+ def test_getasyncgenlocals (self ):
2396+ async def each (lst , a = None ):
2397+ b = (1 , 2 , 3 )
2398+ for v in lst :
2399+ if v == 3 :
2400+ c = 12
2401+ yield v
2402+
2403+ numbers = each ([1 , 2 , 3 ])
2404+ self .assertEqual (inspect .getasyncgenlocals (numbers ),
2405+ {'a' : None , 'lst' : [1 , 2 , 3 ]})
2406+ try :
2407+ next (numbers .__anext__ ())
2408+ except StopIteration :
2409+ pass
2410+ self .assertEqual (inspect .getasyncgenlocals (numbers ),
2411+ {'a' : None , 'lst' : [1 , 2 , 3 ], 'v' : 1 ,
2412+ 'b' : (1 , 2 , 3 )})
2413+ try :
2414+ next (numbers .__anext__ ())
2415+ except StopIteration :
2416+ pass
2417+ self .assertEqual (inspect .getasyncgenlocals (numbers ),
2418+ {'a' : None , 'lst' : [1 , 2 , 3 ], 'v' : 2 ,
2419+ 'b' : (1 , 2 , 3 )})
2420+ try :
2421+ next (numbers .__anext__ ())
2422+ except StopIteration :
2423+ pass
2424+ self .assertEqual (inspect .getasyncgenlocals (numbers ),
2425+ {'a' : None , 'lst' : [1 , 2 , 3 ], 'v' : 3 ,
2426+ 'b' : (1 , 2 , 3 ), 'c' : 12 })
2427+ try :
2428+ next (numbers .__anext__ ())
2429+ except StopAsyncIteration :
2430+ pass
2431+ self .assertEqual (inspect .getasyncgenlocals (numbers ), {})
2432+
2433+ def test_getasyncgenlocals_empty (self ):
2434+ async def yield_one ():
2435+ yield 1
2436+ one = yield_one ()
2437+ self .assertEqual (inspect .getasyncgenlocals (one ), {})
2438+ try :
2439+ next (one .__anext__ ())
2440+ except StopIteration :
2441+ pass
2442+ self .assertEqual (inspect .getasyncgenlocals (one ), {})
2443+ try :
2444+ next (one .__anext__ ())
2445+ except StopAsyncIteration :
2446+ pass
2447+ self .assertEqual (inspect .getasyncgenlocals (one ), {})
2448+
2449+ def test_getasyncgenlocals_error (self ):
2450+ self .assertRaises (TypeError , inspect .getasyncgenlocals , 1 )
2451+ self .assertRaises (TypeError , inspect .getasyncgenlocals , lambda x : True )
2452+ self .assertRaises (TypeError , inspect .getasyncgenlocals , set )
2453+ self .assertRaises (TypeError , inspect .getasyncgenlocals , (2 ,3 ))
2454+
2455+
23242456class MySignature (inspect .Signature ):
23252457 # Top-level to make it picklable;
23262458 # used in test_signature_object_pickle
0 commit comments