File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -94,6 +94,24 @@ async def foo():
9494 coro .throw (ValueError ('boom' ))
9595 self .assertEqual (str (cm .exception ), 'boom' )
9696
97+ def test_lazy_evaluation (self ):
98+ """The body of an async def must not run until the coroutine is first driven."""
99+ log = []
100+
101+ async def noop ():
102+ return
103+
104+ async def lazy ():
105+ log .append ('before' )
106+ await noop ()
107+ log .append ('after' )
108+ return 'done'
109+
110+ coro = lazy ()
111+ self .assertEqual (log , []) # constructing the coroutine runs nothing
112+ self .assertEqual (run_coro (coro ), 'done' )
113+ self .assertEqual (log , ['before' , 'after' ])
114+
97115
98116class AwaitTest (unittest .TestCase ):
99117 """Tests for await expression."""
@@ -139,6 +157,20 @@ async def test():
139157
140158 self .assertEqual (run_coro (test ()), 'done' )
141159
160+ def test_exception_across_await (self ):
161+ """An exception raised in an awaited coroutine propagates and is catchable."""
162+ async def fails ():
163+ raise ValueError ('boom' )
164+
165+ async def test ():
166+ try :
167+ await fails ()
168+ except ValueError as e :
169+ return 'caught: ' + str (e )
170+ return 'not caught'
171+
172+ self .assertEqual (run_coro (test ()), 'caught: boom' )
173+
142174
143175class AsyncWithTest (unittest .TestCase ):
144176 """Tests for async with statement."""
You can’t perform that action at this time.
0 commit comments