|
| 1 | +import asyncio |
1 | 2 | import builtins |
2 | 3 | import collections |
3 | 4 | import datetime |
@@ -65,6 +66,10 @@ def revise(filename, *args): |
65 | 66 | git = mod.StupidGit() |
66 | 67 |
|
67 | 68 |
|
| 69 | +def tearDownModule(): |
| 70 | + asyncio.set_event_loop_policy(None) |
| 71 | + |
| 72 | + |
68 | 73 | def signatures_with_lexicographic_keyword_only_parameters(): |
69 | 74 | """ |
70 | 75 | Yields a whole bunch of functions with only keyword-only parameters, |
@@ -2321,6 +2326,108 @@ async def func(a=None): |
2321 | 2326 | {'a': None, 'gencoro': gencoro, 'b': 'spam'}) |
2322 | 2327 |
|
2323 | 2328 |
|
| 2329 | +class TestGetAsyncGenState(unittest.IsolatedAsyncioTestCase): |
| 2330 | + |
| 2331 | + def setUp(self): |
| 2332 | + async def number_asyncgen(): |
| 2333 | + for number in range(5): |
| 2334 | + yield number |
| 2335 | + self.asyncgen = number_asyncgen() |
| 2336 | + |
| 2337 | + async def asyncTearDown(self): |
| 2338 | + await self.asyncgen.aclose() |
| 2339 | + |
| 2340 | + def _asyncgenstate(self): |
| 2341 | + return inspect.getasyncgenstate(self.asyncgen) |
| 2342 | + |
| 2343 | + def test_created(self): |
| 2344 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_CREATED) |
| 2345 | + |
| 2346 | + async def test_suspended(self): |
| 2347 | + value = await anext(self.asyncgen) |
| 2348 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_SUSPENDED) |
| 2349 | + self.assertEqual(value, 0) |
| 2350 | + |
| 2351 | + async def test_closed_after_exhaustion(self): |
| 2352 | + countdown = 7 |
| 2353 | + with self.assertRaises(StopAsyncIteration): |
| 2354 | + while countdown := countdown - 1: |
| 2355 | + await anext(self.asyncgen) |
| 2356 | + self.assertEqual(countdown, 1) |
| 2357 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_CLOSED) |
| 2358 | + |
| 2359 | + async def test_closed_after_immediate_exception(self): |
| 2360 | + with self.assertRaises(RuntimeError): |
| 2361 | + await self.asyncgen.athrow(RuntimeError) |
| 2362 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_CLOSED) |
| 2363 | + |
| 2364 | + async def test_running(self): |
| 2365 | + async def running_check_asyncgen(): |
| 2366 | + for number in range(5): |
| 2367 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_RUNNING) |
| 2368 | + yield number |
| 2369 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_RUNNING) |
| 2370 | + self.asyncgen = running_check_asyncgen() |
| 2371 | + # Running up to the first yield |
| 2372 | + await anext(self.asyncgen) |
| 2373 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_SUSPENDED) |
| 2374 | + # Running after the first yield |
| 2375 | + await anext(self.asyncgen) |
| 2376 | + self.assertEqual(self._asyncgenstate(), inspect.AGEN_SUSPENDED) |
| 2377 | + |
| 2378 | + def test_easy_debugging(self): |
| 2379 | + # repr() and str() of a asyncgen state should contain the state name |
| 2380 | + names = 'AGEN_CREATED AGEN_RUNNING AGEN_SUSPENDED AGEN_CLOSED'.split() |
| 2381 | + for name in names: |
| 2382 | + state = getattr(inspect, name) |
| 2383 | + self.assertIn(name, repr(state)) |
| 2384 | + self.assertIn(name, str(state)) |
| 2385 | + |
| 2386 | + async def test_getasyncgenlocals(self): |
| 2387 | + async def each(lst, a=None): |
| 2388 | + b=(1, 2, 3) |
| 2389 | + for v in lst: |
| 2390 | + if v == 3: |
| 2391 | + c = 12 |
| 2392 | + yield v |
| 2393 | + |
| 2394 | + numbers = each([1, 2, 3]) |
| 2395 | + self.assertEqual(inspect.getasyncgenlocals(numbers), |
| 2396 | + {'a': None, 'lst': [1, 2, 3]}) |
| 2397 | + await anext(numbers) |
| 2398 | + self.assertEqual(inspect.getasyncgenlocals(numbers), |
| 2399 | + {'a': None, 'lst': [1, 2, 3], 'v': 1, |
| 2400 | + 'b': (1, 2, 3)}) |
| 2401 | + await anext(numbers) |
| 2402 | + self.assertEqual(inspect.getasyncgenlocals(numbers), |
| 2403 | + {'a': None, 'lst': [1, 2, 3], 'v': 2, |
| 2404 | + 'b': (1, 2, 3)}) |
| 2405 | + await anext(numbers) |
| 2406 | + self.assertEqual(inspect.getasyncgenlocals(numbers), |
| 2407 | + {'a': None, 'lst': [1, 2, 3], 'v': 3, |
| 2408 | + 'b': (1, 2, 3), 'c': 12}) |
| 2409 | + with self.assertRaises(StopAsyncIteration): |
| 2410 | + await anext(numbers) |
| 2411 | + self.assertEqual(inspect.getasyncgenlocals(numbers), {}) |
| 2412 | + |
| 2413 | + async def test_getasyncgenlocals_empty(self): |
| 2414 | + async def yield_one(): |
| 2415 | + yield 1 |
| 2416 | + one = yield_one() |
| 2417 | + self.assertEqual(inspect.getasyncgenlocals(one), {}) |
| 2418 | + await anext(one) |
| 2419 | + self.assertEqual(inspect.getasyncgenlocals(one), {}) |
| 2420 | + with self.assertRaises(StopAsyncIteration): |
| 2421 | + await anext(one) |
| 2422 | + self.assertEqual(inspect.getasyncgenlocals(one), {}) |
| 2423 | + |
| 2424 | + def test_getasyncgenlocals_error(self): |
| 2425 | + self.assertRaises(TypeError, inspect.getasyncgenlocals, 1) |
| 2426 | + self.assertRaises(TypeError, inspect.getasyncgenlocals, lambda x: True) |
| 2427 | + self.assertRaises(TypeError, inspect.getasyncgenlocals, set) |
| 2428 | + self.assertRaises(TypeError, inspect.getasyncgenlocals, (2,3)) |
| 2429 | + |
| 2430 | + |
2324 | 2431 | class MySignature(inspect.Signature): |
2325 | 2432 | # Top-level to make it picklable; |
2326 | 2433 | # used in test_signature_object_pickle |
|
0 commit comments