1# Test that tasks return their value correctly to the caller 2 3try: 4 import uasyncio as asyncio 5except ImportError: 6 try: 7 import asyncio 8 except ImportError: 9 print("SKIP") 10 raise SystemExit 11 12 13def custom_handler(loop, context): 14 print("custom_handler", repr(context["exception"])) 15 16 17async def task(i): 18 # Raise with 2 args so exception prints the same in uPy and CPython 19 raise ValueError(i, i + 1) 20 21 22async def main(): 23 loop = asyncio.get_event_loop() 24 25 # Check default exception handler, should be None 26 print(loop.get_exception_handler()) 27 28 # Set exception handler and test it was set 29 loop.set_exception_handler(custom_handler) 30 print(loop.get_exception_handler() == custom_handler) 31 32 # Create a task that raises and uses the custom exception handler 33 asyncio.create_task(task(0)) 34 print("sleep") 35 for _ in range(2): 36 await asyncio.sleep(0) 37 38 # Create 2 tasks to test order of printing exception 39 asyncio.create_task(task(1)) 40 asyncio.create_task(task(2)) 41 print("sleep") 42 for _ in range(2): 43 await asyncio.sleep(0) 44 45 # Create a task, let it run, then await it (no exception should be printed) 46 t = asyncio.create_task(task(3)) 47 await asyncio.sleep(0) 48 try: 49 await t 50 except ValueError as er: 51 print(repr(er)) 52 53 print("done") 54 55 56asyncio.run(main()) 57