summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_peepholer.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-02-12 10:18:59 (GMT)
committerGitHub <noreply@github.com>2020-02-12 10:18:59 (GMT)
commit8c579b1cc86053473eb052b76327279476740c9b (patch)
treebc99d5f52e1330500216512d1064c2f341b64d89 /Lib/test/test_peepholer.py
parent0cc6b5e559b8303b18fdd56c2befd900fe7b5e35 (diff)
downloadcpython-8c579b1cc86053473eb052b76327279476740c9b.zip
cpython-8c579b1cc86053473eb052b76327279476740c9b.tar.gz
cpython-8c579b1cc86053473eb052b76327279476740c9b.tar.bz2
bpo-32856: Optimize the assignment idiom in comprehensions. (GH-16814)
Now `for y in [expr]` in comprehensions is as fast as a simple assignment `y = expr`.
Diffstat (limited to 'Lib/test/test_peepholer.py')
-rw-r--r--Lib/test/test_peepholer.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index 567e6a1..7913e91 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -495,6 +495,20 @@ class TestTranforms(BytecodeTestCase):
return 6
self.check_lnotab(f)
+ def test_assignment_idiom_in_comprehensions(self):
+ def listcomp():
+ return [y for x in a for y in [f(x)]]
+ self.assertEqual(count_instr_recursively(listcomp, 'FOR_ITER'), 1)
+ def setcomp():
+ return {y for x in a for y in [f(x)]}
+ self.assertEqual(count_instr_recursively(setcomp, 'FOR_ITER'), 1)
+ def dictcomp():
+ return {y: y for x in a for y in [f(x)]}
+ self.assertEqual(count_instr_recursively(dictcomp, 'FOR_ITER'), 1)
+ def genexpr():
+ return (y for x in a for y in [f(x)])
+ self.assertEqual(count_instr_recursively(genexpr, 'FOR_ITER'), 1)
+
class TestBuglets(unittest.TestCase):