Skip to content

Commit 7c159f3

Browse files
committed
Test that an AttributeError from the generator is not swallowed
1 parent 2b4d25d commit 7c159f3

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎Lib/test/test_contextlib.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ def woohoo():
212212
self.assertEqual(state, ['enter'])
213213
self.assertEqual(state, ['enter', 'exit'])
214214

215+
def test_contextmanager_preserves_generator_attributeerror(self):
216+
# Only the attribute deletion is guarded: an AttributeError raised
217+
# by the generator itself must propagate unchanged.
218+
exc = AttributeError('generator failure')
219+
@contextmanager
220+
def woohoo():
221+
raise exc
222+
yield
223+
with self.assertRaises(AttributeError) as cm:
224+
with woohoo():
225+
pass
226+
self.assertIs(cm.exception, exc)
227+
215228
def test_contextmanager_trap_second_yield(self):
216229
@contextmanager
217230
def whoo():

‎Lib/test/test_contextlib_async.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,20 @@ async def woohoo():
254254
self.assertEqual(state, ['enter'])
255255
self.assertEqual(state, ['enter', 'exit'])
256256

257+
@_async_test
258+
async def test_contextmanager_preserves_generator_attributeerror(self):
259+
# Only the attribute deletion is guarded: an AttributeError raised
260+
# by the generator itself must propagate unchanged.
261+
exc = AttributeError('generator failure')
262+
@asynccontextmanager
263+
async def woohoo():
264+
raise exc
265+
yield
266+
with self.assertRaises(AttributeError) as cm:
267+
async with woohoo():
268+
pass
269+
self.assertIs(cm.exception, exc)
270+
257271
@_async_test
258272
async def test_contextmanager_trap_second_yield(self):
259273
@asynccontextmanager

0 commit comments

Comments
 (0)