Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(70464)

Side by Side Diff: Lib/test/test_generators.py

Issue 25612: nested try..excepts don't work correctly for generators
Patch Set: Created 3 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Python/ceval.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import gc 1 import gc
2 import sys 2 import sys
3 import unittest 3 import unittest
4 import warnings 4 import warnings
5 import weakref 5 import weakref
6 import inspect 6 import inspect
7 import types 7 import types
8 8
9 from test import support 9 from test import support
10 10
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 self.assertEqual(next(g), 1) 252 self.assertEqual(next(g), 1)
253 253
254 with self.assertWarnsRegex(PendingDeprecationWarning, "StopIteration"): 254 with self.assertWarnsRegex(PendingDeprecationWarning, "StopIteration"):
255 with self.assertRaises(StopIteration): 255 with self.assertRaises(StopIteration):
256 next(g) 256 next(g)
257 257
258 with self.assertRaises(StopIteration): 258 with self.assertRaises(StopIteration):
259 # This time StopIteration isn't raised from the generator's body, 259 # This time StopIteration isn't raised from the generator's body,
260 # hence no warning. 260 # hence no warning.
261 next(g) 261 next(g)
262
263 def test_nested_try_1(self):
264 class MainError(Exception):
265 pass
266
267 class SubError(Exception):
268 pass
269
270 def main():
271 try:
272 raise MainError()
273 except MainError:
274 try:
275 yield
276 except SubError:
277 pass
278 raise
279
280 coro = main()
281 coro.send(None)
282 try:
283 coro.throw(SubError())
284 except MainError as ex:
285 self.assertIsNone(ex.__cause__)
286 self.assertIsNone(ex.__context__)
287 except Exception as ex:
288 self.fail('MainError expected; got {!r}'.format(ex))
289 else:
290 self.fail('MainError expected; got no exception')
291
292 def test_nested_try_2(self):
293 class MainError(Exception):
294 pass
295
296 class SubError(Exception):
297 pass
298
299 def main():
300 try:
301 raise MainError()
302 except MainError:
303 yield
304
305 coro = main()
306 coro.send(None)
307 try:
308 coro.throw(SubError())
309 except SubError as ex:
310 self.assertIsNone(ex.__cause__)
311 self.assertIs(type(ex.__context__), MainError)
312 except Exception as ex:
313 self.fail('SubError expected; got {!r}'.format(ex))
314 else:
315 self.fail('SubError expected; got no exception')
316
317 def test_nested_try_3(self):
318 class MainError(Exception):
319 pass
320
321 class SubError(Exception):
322 pass
323
324 def main():
325 try:
326 raise MainError()
327 except MainError as ex:
328 try:
329 yield
330 except SubError:
331 pass
332 raise ex
333
334 coro = main()
335 coro.send(None)
336 try:
337 coro.throw(SubError())
338 except MainError as ex:
339 self.assertIsNone(ex.__cause__)
340 self.assertIsNone(ex.__context__)
341 except Exception as ex:
342 self.fail('MainError expected; got {!r}'.format(ex))
343 else:
344 self.fail('MainError expected; got no exception')
262 345
263 346
264 class YieldFromTests(unittest.TestCase): 347 class YieldFromTests(unittest.TestCase):
265 def test_generator_gi_yieldfrom(self): 348 def test_generator_gi_yieldfrom(self):
266 def a(): 349 def a():
267 self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_RUNNI NG) 350 self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_RUNNI NG)
268 self.assertIsNone(gen_b.gi_yieldfrom) 351 self.assertIsNone(gen_b.gi_yieldfrom)
269 yield 352 yield
270 self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_RUNNI NG) 353 self.assertEqual(inspect.getgeneratorstate(gen_b), inspect.GEN_RUNNI NG)
271 self.assertIsNone(gen_b.gi_yieldfrom) 354 self.assertIsNone(gen_b.gi_yieldfrom)
(...skipping 1884 matching lines...) Expand 10 before | Expand all | Expand 10 after
2156 # Note that doctest and regrtest both look in sys.argv for a "-v" argument, 2239 # Note that doctest and regrtest both look in sys.argv for a "-v" argument,
2157 # so this works as expected in both ways of running regrtest. 2240 # so this works as expected in both ways of running regrtest.
2158 def test_main(verbose=None): 2241 def test_main(verbose=None):
2159 from test import support, test_generators 2242 from test import support, test_generators
2160 support.run_unittest(__name__) 2243 support.run_unittest(__name__)
2161 support.run_doctest(test_generators, verbose) 2244 support.run_doctest(test_generators, verbose)
2162 2245
2163 # This part isn't needed for regrtest, but for running the test directly. 2246 # This part isn't needed for regrtest, but for running the test directly.
2164 if __name__ == "__main__": 2247 if __name__ == "__main__":
2165 test_main(1) 2248 test_main(1)
OLDNEW
« no previous file with comments | « no previous file | Python/ceval.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+