summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-03-11 08:54:47 (GMT)
committerGitHub <noreply@github.com>2018-03-11 08:54:47 (GMT)
commit3f7e9aa2ef215917b9f1521441f67f4ecd33a1bc (patch)
tree7f4b2918d822c863869ef604332e851eeea5c0a1 /Lib
parent4e2442505c5e9eec396dcef4d2e6bdd2b6f92fc9 (diff)
downloadcpython-3f7e9aa2ef215917b9f1521441f67f4ecd33a1bc.zip
cpython-3f7e9aa2ef215917b9f1521441f67f4ecd33a1bc.tar.gz
cpython-3f7e9aa2ef215917b9f1521441f67f4ecd33a1bc.tar.bz2
bpo-32925: Optimized iterating and containing test for literal lists (GH-5842)
consisting of non-constants: `x in [a, b]` and `for x in [a, b]`. The case of all constant elements already was optimized.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_peepholer.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index c24cf77..794d104 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -3,6 +3,19 @@ import unittest
from test.bytecode_helper import BytecodeTestCase
+def count_instr_recursively(f, opname):
+ count = 0
+ for instr in dis.get_instructions(f):
+ if instr.opname == opname:
+ count += 1
+ if hasattr(f, '__code__'):
+ f = f.__code__
+ for c in f.co_consts:
+ if hasattr(c, 'co_code'):
+ count += count_instr_recursively(c, opname)
+ return count
+
+
class TestTranforms(BytecodeTestCase):
def test_unot(self):
@@ -311,6 +324,17 @@ class TestTranforms(BytecodeTestCase):
self.assertFalse(instr.opname.startswith('BINARY_'))
self.assertFalse(instr.opname.startswith('BUILD_'))
+ def test_in_literal_list(self):
+ def containtest():
+ return x in [a, b]
+ self.assertEqual(count_instr_recursively(containtest, 'BUILD_LIST'), 0)
+
+ def test_iterate_literal_list(self):
+ def forloop():
+ for x in [a, b]:
+ pass
+ self.assertEqual(count_instr_recursively(forloop, 'BUILD_LIST'), 0)
+
class TestBuglets(unittest.TestCase):