summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2022-11-07 14:49:51 (GMT)
committerGitHub <noreply@github.com>2022-11-07 14:49:51 (GMT)
commit4a1c58d504a49eeb9be7beef3ca861a9d6b28ede (patch)
tree8c5245a6a3a19a311d0d5a31723da1225a2c6795 /Lib
parent80c08d1cd67afdd1336c65ba23a044b6ac490f33 (diff)
downloadcpython-4a1c58d504a49eeb9be7beef3ca861a9d6b28ede.zip
cpython-4a1c58d504a49eeb9be7beef3ca861a9d6b28ede.tar.gz
cpython-4a1c58d504a49eeb9be7beef3ca861a9d6b28ede.tar.bz2
GH-96793: Specialize FOR_ITER for generators. (GH-98772)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/opcode.py1
-rw-r--r--Lib/test/test_generators.py20
2 files changed, 21 insertions, 0 deletions
diff --git a/Lib/opcode.py b/Lib/opcode.py
index dfe06f8..990f5b6 100644
--- a/Lib/opcode.py
+++ b/Lib/opcode.py
@@ -328,6 +328,7 @@ _specializations = {
"FOR_ITER_ADAPTIVE",
"FOR_ITER_LIST",
"FOR_ITER_RANGE",
+ "FOR_ITER_GEN",
],
"LOAD_ATTR": [
"LOAD_ATTR_ADAPTIVE",
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index 42cc20c..492b77a 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -306,6 +306,26 @@ class ExceptionTest(unittest.TestCase):
self.assertEqual(next(g), "done")
self.assertEqual(sys.exc_info(), (None, None, None))
+ def test_nested_gen_except_loop(self):
+ def gen():
+ for i in range(100):
+ self.assertIsInstance(sys.exception(), TypeError)
+ yield "doing"
+
+ def outer():
+ try:
+ raise TypeError
+ except:
+ for x in gen():
+ yield x
+
+ try:
+ raise ValueError
+ except Exception:
+ for x in outer():
+ self.assertEqual(x, "doing")
+ self.assertEqual(sys.exception(), None)
+
def test_except_throw_exception_context(self):
def gen():
try: