summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_peepholer.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-03-11 16:27:02 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-03-11 16:27:02 (GMT)
commit17b880a5d6b64f1e7d26ec8229e8b41cdf740690 (patch)
tree48914ff19d84e0a6802a5a14af640122a0e5bb58 /Lib/test/test_peepholer.py
parent336c729563a3c0ec0d4955c59100ad772e9feb48 (diff)
downloadcpython-17b880a5d6b64f1e7d26ec8229e8b41cdf740690.zip
cpython-17b880a5d6b64f1e7d26ec8229e8b41cdf740690.tar.gz
cpython-17b880a5d6b64f1e7d26ec8229e8b41cdf740690.tar.bz2
Issue #11244: The peephole optimizer is now able to constant-fold
arbitrarily complex expressions. This also fixes a 3.2 regression where operations involving negative numbers were not constant-folded.
Diffstat (limited to 'Lib/test/test_peepholer.py')
-rw-r--r--Lib/test/test_peepholer.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index 531b425..a9eb23f 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -8,8 +8,10 @@ def disassemble(func):
f = StringIO()
tmp = sys.stdout
sys.stdout = f
- dis.dis(func)
- sys.stdout = tmp
+ try:
+ dis.dis(func)
+ finally:
+ sys.stdout = tmp
result = f.getvalue()
f.close()
return result
@@ -99,6 +101,12 @@ class TestTranforms(unittest.TestCase):
self.assertIn(elem, asm)
self.assertNotIn('BUILD_TUPLE', asm)
+ # Long tuples should be folded too.
+ asm = dis_single(repr(tuple(range(10000))))
+ # One LOAD_CONST for the tuple, one for the None return value
+ self.assertEqual(asm.count('LOAD_CONST'), 2)
+ self.assertNotIn('BUILD_TUPLE', asm)
+
# Bug 1053819: Tuple of constants misidentified when presented with:
# . . . opcode_with_arg 100 unary_opcode BUILD_TUPLE 1 . . .
# The following would segfault upon compilation
@@ -267,6 +275,25 @@ class TestTranforms(unittest.TestCase):
asm = disassemble(f)
self.assertNotIn('BINARY_ADD', asm)
+ def test_constant_folding(self):
+ # Issue #11244: aggressive constant folding.
+ exprs = [
+ "3 * -5",
+ "-3 * 5",
+ "2 * (3 * 4)",
+ "(2 * 3) * 4",
+ "(-1, 2, 3)",
+ "(1, -2, 3)",
+ "(1, 2, -3)",
+ "(1, 2, -3) * 6",
+ "lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}",
+ ]
+ for e in exprs:
+ asm = dis_single(e)
+ self.assertNotIn('UNARY_', asm, e)
+ self.assertNotIn('BINARY_', asm, e)
+ self.assertNotIn('BUILD_', asm, e)
+
def test_main(verbose=None):
import sys