8000 Add more tests for throwing into `yield from` by brandtbucher · Pull Request #94097 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Add more tests for throwing into yield from #94097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use assertIsNone
  • Loading branch information
brandtbucher committed Jun 22, 2022
commit 586244c763b604e7239c2c55161b4879e5fe995b
68 changes: 34 additions & 34 deletions Lib/test/test_yield_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,8 @@ class TestInterestingEdgeCases(unittest.TestCase):
def assert_stop_iteration(self, iterator):
with self.assertRaises(StopIteration) as caught:
next(iterator)
self.assertIs(caught.exception.value, None)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.value)
self.assertIsNone(caught.exception.__context__)

def assert_generator_raised_stop_iteration(self):
return self.assertRaisesRegex(RuntimeError, r"^generator raised StopIteration$")
Expand Down Expand Up @@ -1090,7 +1090,7 @@ def outer():
with self.assertRaises(GeneratorExit) as caught:
g.throw(thrown)
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1101,7 +1101,7 @@ def outer():
with self.assert_generator_raised_stop_iteration() as caught:
g.throw(thrown)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1111,7 +1111,7 @@ def outer():
with self.assertRaises(BaseException) as caught:
g.throw(thrown)
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1121,7 +1121,7 @@ def outer():
with self.assertRaises(Exception) as caught:
g.throw(thrown)
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

def test_close_and_throw_raise_generator_exit(self):
Expand Down Expand Up @@ -1161,7 +1161,7 @@ def outer():
# propagates. This is consistent with PEP 380:
# https://peps.python.org/pep-0380/#proposal
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1173,7 +1173,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1185,7 +1185,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1197,7 +1197,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

def test_close_and_throw_raise_stop_iteration(self):
Expand Down Expand Up @@ -1226,7 +1226,7 @@ def outer():
g.close()
self.assertIs(caught.exception.__context__, raised)
self.assertIsInstance(caught.exception.__context__.__context__, GeneratorExit)
self.assertIs(caught.exception.__context__.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw GeneratorExit"):
Expand All @@ -1241,7 +1241,7 @@ def outer():
# This isn't the same GeneratorExit as thrown! It's the one created
# by calling inner.close():
self.assertIsInstance(caught.exception.__context__.__context__, GeneratorExit)
self.assertIs(caught.exception.__context__.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1254,7 +1254,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception.__context__, raised)
self.assertIs(caught.exception.__context__.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1267,7 +1267,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception.__context__, raised)
self.assertIs(caught.exception.__context__.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1280,7 +1280,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception.__context__, raised)
self.assertIs(caught.exception.__context__.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__.__context__)
self.assert_stop_iteration(g)

def test_close_and_throw_raise_base_exception(self):
Expand Down Expand Up @@ -1308,7 +1308,7 @@ def outer():
g.close()
self.assertIs(caught.exception, raised)
self.assertIsInstance(caught.exception.__context__, GeneratorExit)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw GeneratorExit"):
Expand All @@ -1322,7 +1322,7 @@ def outer():
# This isn't the same GeneratorExit as thrown! It's the one created
# by calling inner.close():
self.assertIsInstance(caught.exception.__context__, GeneratorExit)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1334,7 +1334,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1346,7 +1346,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1358,7 +1358,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

def test_close_and_throw_raise_exception(self):
Expand Down Expand Up @@ -1386,7 +1386,7 @@ def outer():
g.close()
self.assertIs(caught.exception, raised)
self.assertIsInstance(caught.exception.__context__, GeneratorExit)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw GeneratorExit"):
Expand All @@ -1400,7 +1400,7 @@ def outer():
# This isn't the same GeneratorExit as thrown! It's the one created
# by calling inner.close():
self.assertIsInstance(caught.exception.__context__, GeneratorExit)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1412,7 +1412,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1424,7 +1424,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1436,7 +1436,7 @@ def outer():
g.throw(thrown)
self.assertIs(caught.exception, raised)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

def test_close_and_throw_yield(self):
Expand All @@ -1462,7 +1462,7 @@ def outer():
# https://peps.python.org/pep-0342/#new-generator-method-close
with self.assert_generator_ignored_generator_exit() as caught:
g.close()
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw GeneratorExit"):
Expand All @@ -1473,7 +1473,7 @@ def outer():
# https://peps.python.org/pep-0342/#new-generator-method-close
with self.assert_generator_ignored_generator_exit() as caught:
g.throw(thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1485,7 +1485,7 @@ def outer():
with self.assert_generator_raised_stop_iteration() as caught:
next(g)
self.assertIs(caught.exception.__context__, thrown)
self.assertIs(caught.exception.__context__.__context__, None)
self.assertIsNone(caught.exception.__context__.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1496,7 +1496,7 @@ def outer():
with self.assertRaises(BaseException) as caught:
next(g)
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1507,7 +1507,7 @@ def outer():
with self.assertRaises(Exception) as caught:
next(g)
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

def test_close_and_throw_return(self):
Expand Down Expand Up @@ -1543,7 +1543,7 @@ def outer():
with self.assertRaises(GeneratorExit) as caught:
g.throw(thrown)
self.assertIs(caught.exception, thrown)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw StopIteration"):
Expand All @@ -1553,7 +1553,7 @@ def outer():
with self.assertRaises(StopIteration) as caught:
g.throw(thrown)
self.assertIs(caught.exception.value, returned)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw BaseException"):
Expand All @@ -1563,7 +1563,7 @@ def outer():
with self.assertRaises(StopIteration) as caught:
g.throw(thrown)
self.assertIs(caught.exception.value, returned)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

with self.subTest("throw Exception"):
Expand All @@ -1573,7 +1573,7 @@ def outer():
with self.assertRaises(StopIteration) as caught:
g.throw(thrown)
self.assertIs(caught.exception.value, returned)
self.assertIs(caught.exception.__context__, None)
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)


Expand Down
0