Skip to content

Commit adf2bf3

Browse files
committed
test for the Stop(Async)Iteration case
1 parent 2990340 commit adf2bf3

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

Lib/test/test_contextlib.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,41 @@ def f():
104104
self.assertEqual(frames[0].line, '1/0')
105105

106106
# Repeat with RuntimeError (which goes through a different code path)
107+
class RuntimeErrorSubclass(RuntimeError):
108+
pass
109+
107110
try:
108111
with f():
109-
raise NotImplementedError(42)
110-
except NotImplementedError as e:
112+
raise RuntimeErrorSubclass(42)
113+
except RuntimeErrorSubclass as e:
111114
frames = traceback.extract_tb(e.__traceback__)
112115

113116
self.assertEqual(len(frames), 1)
114117
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
115-
self.assertEqual(frames[0].line, 'raise NotImplementedError(42)')
118+
self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)')
119+
120+
class StopIterationSubclass(StopIteration):
121+
pass
122+
123+
for stop_exc in (
124+
StopIteration('spam'),
125+
StopIterationSubclass('spam'),
126+
):
127+
with self.subTest(type=type(stop_exc)):
128+
try:
129+
with f():
130+
raise stop_exc
131+
except type(stop_exc) as e:
132+
self.assertIs(e, stop_exc)
133+
frames = traceback.extract_tb(e.__traceback__)
134+
else:
135+
self.fail(f'{stop_exc} was suppressed')
136+
137+
self.assertEqual(len(frames), 2)
138+
self.assertEqual(frames[0].name, 'f')
139+
self.assertEqual(frames[0].line, 'yield')
140+
self.assertEqual(frames[1].name, 'test_contextmanager_traceback')
141+
self.assertEqual(frames[1].line, 'raise stop_exc')
116142

117143
def test_contextmanager_no_reraise(self):
118144
@contextmanager

Lib/test/test_contextlib_async.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,46 @@ async def f():
143143
self.assertEqual(frames[0].line, '1/0')
144144

145145
# Repeat with RuntimeError (which goes through a different code path)
146+
class RuntimeErrorSubclass(RuntimeError):
147+
pass
148+
146149
try:
147150
async with f():
148-
raise NotImplementedError(42)
149-
except NotImplementedError as e:
151+
raise RuntimeErrorSubclass(42)
152+
except RuntimeErrorSubclass as e:
150153
frames = traceback.extract_tb(e.__traceback__)
151154

152155
self.assertEqual(len(frames), 1)
153156
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
154-
self.assertEqual(frames[0].line, 'raise NotImplementedError(42)')
157+
self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)')
158+
159+
class StopIterationSubclass(StopIteration):
160+
pass
161+
162+
class StopAsyncIterationSubclass(StopAsyncIteration):
163+
pass
164+
165+
for stop_exc in (
166+
StopIteration('spam'),
167+
StopAsyncIteration('ham'),
168+
StopIterationSubclass('spam'),
169+
StopAsyncIterationSubclass('spam')
170+
):
171+
with self.subTest(type=type(stop_exc)):
172+
try:
173+
async with f():
174+
raise stop_exc
175+
except type(stop_exc) as e:
176+
self.assertIs(e, stop_exc)
177+
frames = traceback.extract_tb(e.__traceback__)
178+
else:
179+
self.fail(f'{stop_exc} was suppressed')
180+
181+
self.assertEqual(len(frames), 2)
182+
self.assertEqual(frames[0].name, 'f')
183+
self.assertEqual(frames[0].line, 'yield')
184+
self.assertEqual(frames[1].name, 'test_contextmanager_traceback')
185+
self.assertEqual(frames[1].line, 'raise stop_exc')
155186

156187
@_async_test
157188
async def test_contextmanager_no_reraise(self):

0 commit comments

Comments
 (0)