2004-08-23 20:37:48 -03:00
|
|
|
import dis
|
|
|
|
import sys
|
|
|
|
from cStringIO import StringIO
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
def disassemble(func):
|
|
|
|
f = StringIO()
|
|
|
|
tmp = sys.stdout
|
|
|
|
sys.stdout = f
|
|
|
|
dis.dis(func)
|
|
|
|
sys.stdout = tmp
|
|
|
|
result = f.getvalue()
|
|
|
|
f.close()
|
|
|
|
return result
|
|
|
|
|
|
|
|
def dis_single(line):
|
|
|
|
return disassemble(compile(line, '', 'single'))
|
|
|
|
|
|
|
|
class TestTranforms(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_unot(self):
|
2009-02-28 15:03:21 -04:00
|
|
|
# UNARY_NOT POP_JUMP_IF_FALSE --> POP_JUMP_IF_TRUE
|
2004-08-23 20:37:48 -03:00
|
|
|
def unot(x):
|
|
|
|
if not x == 2:
|
|
|
|
del x
|
|
|
|
asm = disassemble(unot)
|
2009-02-28 15:03:21 -04:00
|
|
|
for elem in ('UNARY_NOT', 'POP_JUMP_IF_FALSE'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem not in asm)
|
|
|
|
self.assertTrue('POP_JUMP_IF_TRUE' in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
def test_elim_inversion_of_is_or_in(self):
|
|
|
|
for line, elem in (
|
|
|
|
('not a is b', '(is not)',),
|
|
|
|
('not a in b', '(not in)',),
|
|
|
|
('not a is not b', '(is)',),
|
|
|
|
('not a not in b', '(in)',),
|
|
|
|
):
|
|
|
|
asm = dis_single(line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
def test_none_as_constant(self):
|
|
|
|
# LOAD_GLOBAL None --> LOAD_CONST None
|
|
|
|
def f(x):
|
2004-08-26 02:23:19 -03:00
|
|
|
None
|
|
|
|
return x
|
2004-08-23 20:37:48 -03:00
|
|
|
asm = disassemble(f)
|
|
|
|
for elem in ('LOAD_GLOBAL',):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem not in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
for elem in ('LOAD_CONST', '(None)'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm)
|
2007-03-02 15:20:46 -04:00
|
|
|
def f():
|
|
|
|
'Adding a docstring made this test fail in Py2.5.0'
|
|
|
|
return None
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('LOAD_CONST' in disassemble(f))
|
|
|
|
self.assertTrue('LOAD_GLOBAL' not in disassemble(f))
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
def test_while_one(self):
|
2009-02-28 15:03:21 -04:00
|
|
|
# Skip over: LOAD_CONST trueconst POP_JUMP_IF_FALSE xx
|
2004-08-23 20:37:48 -03:00
|
|
|
def f():
|
2004-08-26 02:23:19 -03:00
|
|
|
while 1:
|
|
|
|
pass
|
|
|
|
return list
|
2004-08-23 20:37:48 -03:00
|
|
|
asm = disassemble(f)
|
2009-02-28 15:03:21 -04:00
|
|
|
for elem in ('LOAD_CONST', 'POP_JUMP_IF_FALSE'):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem not in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
for elem in ('JUMP_ABSOLUTE',):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
def test_pack_unpack(self):
|
|
|
|
for line, elem in (
|
2004-09-22 15:44:21 -03:00
|
|
|
('a, = a,', 'LOAD_CONST',),
|
|
|
|
('a, b = a, b', 'ROT_TWO',),
|
|
|
|
('a, b, c = a, b, c', 'ROT_THREE',),
|
2004-08-23 20:37:48 -03:00
|
|
|
):
|
|
|
|
asm = dis_single(line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm)
|
|
|
|
self.assertTrue('BUILD_TUPLE' not in asm)
|
|
|
|
self.assertTrue('UNPACK_TUPLE' not in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
|
2004-09-22 15:44:21 -03:00
|
|
|
def test_folding_of_tuples_of_constants(self):
|
|
|
|
for line, elem in (
|
2004-11-02 00:20:10 -04:00
|
|
|
('a = 1,2,3', '((1, 2, 3))'),
|
|
|
|
('("a","b","c")', "(('a', 'b', 'c'))"),
|
|
|
|
('a,b,c = 1,2,3', '((1, 2, 3))'),
|
|
|
|
('(None, 1, None)', '((None, 1, None))'),
|
|
|
|
('((1, 2), 3, 4)', '(((1, 2), 3, 4))'),
|
2004-09-22 15:44:21 -03:00
|
|
|
):
|
|
|
|
asm = dis_single(line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm)
|
|
|
|
self.assertTrue('BUILD_TUPLE' not in asm)
|
2004-09-22 15:44:21 -03:00
|
|
|
|
2004-10-26 05:59:14 -03:00
|
|
|
# Bug 1053819: Tuple of constants misidentified when presented with:
|
|
|
|
# . . . opcode_with_arg 100 unary_opcode BUILD_TUPLE 1 . . .
|
|
|
|
# The following would segfault upon compilation
|
|
|
|
def crater():
|
|
|
|
(~[
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
],)
|
|
|
|
|
2005-01-02 02:17:33 -04:00
|
|
|
def test_folding_of_binops_on_constants(self):
|
|
|
|
for line, elem in (
|
|
|
|
('a = 2+3+4', '(9)'), # chained fold
|
|
|
|
('"@"*4', "('@@@@')"), # check string ops
|
|
|
|
('a="abc" + "def"', "('abcdef')"), # check string ops
|
|
|
|
('a = 3**4', '(81)'), # binary power
|
|
|
|
('a = 3*4', '(12)'), # binary multiply
|
|
|
|
('a = 13//4', '(3)'), # binary floor divide
|
|
|
|
('a = 14%4', '(2)'), # binary modulo
|
|
|
|
('a = 2+3', '(5)'), # binary add
|
|
|
|
('a = 13-4', '(9)'), # binary subtract
|
|
|
|
('a = (12,13)[1]', '(13)'), # binary subscr
|
|
|
|
('a = 13 << 2', '(52)'), # binary lshift
|
|
|
|
('a = 13 >> 2', '(3)'), # binary rshift
|
|
|
|
('a = 13 & 7', '(5)'), # binary and
|
|
|
|
('a = 13 ^ 7', '(10)'), # binary xor
|
|
|
|
('a = 13 | 7', '(15)'), # binary or
|
|
|
|
):
|
|
|
|
asm = dis_single(line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm, asm)
|
|
|
|
self.assertTrue('BINARY_' not in asm)
|
2005-01-02 02:17:33 -04:00
|
|
|
|
|
|
|
# Verify that unfoldables are skipped
|
|
|
|
asm = dis_single('a=2+"b"')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('(2)' in asm)
|
|
|
|
self.assertTrue("('b')" in asm)
|
2005-01-02 02:17:33 -04:00
|
|
|
|
2005-01-26 08:50:05 -04:00
|
|
|
# Verify that large sequences do not result from folding
|
|
|
|
asm = dis_single('a="x"*1000')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('(1000)' in asm)
|
2005-01-26 08:50:05 -04:00
|
|
|
|
2005-02-20 08:46:54 -04:00
|
|
|
def test_folding_of_unaryops_on_constants(self):
|
|
|
|
for line, elem in (
|
|
|
|
('`1`', "('1')"), # unary convert
|
|
|
|
('-0.5', '(-0.5)'), # unary negative
|
|
|
|
('~-2', '(1)'), # unary invert
|
|
|
|
):
|
|
|
|
asm = dis_single(line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm, asm)
|
|
|
|
self.assertTrue('UNARY_' not in asm)
|
2005-02-20 08:46:54 -04:00
|
|
|
|
|
|
|
# Verify that unfoldables are skipped
|
|
|
|
for line, elem in (
|
|
|
|
('-"abc"', "('abc')"), # unary negative
|
|
|
|
('~"abc"', "('abc')"), # unary invert
|
|
|
|
):
|
|
|
|
asm = dis_single(line)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in asm, asm)
|
|
|
|
self.assertTrue('UNARY_' in asm)
|
2005-02-20 08:46:54 -04:00
|
|
|
|
2004-08-23 20:37:48 -03:00
|
|
|
def test_elim_extra_return(self):
|
|
|
|
# RETURN LOAD_CONST None RETURN --> RETURN
|
|
|
|
def f(x):
|
|
|
|
return x
|
|
|
|
asm = disassemble(f)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('LOAD_CONST' not in asm)
|
|
|
|
self.assertTrue('(None)' not in asm)
|
2004-08-23 20:37:48 -03:00
|
|
|
self.assertEqual(asm.split().count('RETURN_VALUE'), 1)
|
|
|
|
|
2006-10-14 18:33:38 -03:00
|
|
|
def test_elim_jump_to_return(self):
|
|
|
|
# JUMP_FORWARD to RETURN --> RETURN
|
|
|
|
def f(cond, true_value, false_value):
|
|
|
|
return true_value if cond else false_value
|
|
|
|
asm = disassemble(f)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('JUMP_FORWARD' not in asm)
|
|
|
|
self.assertTrue('JUMP_ABSOLUTE' not in asm)
|
2006-10-14 18:33:38 -03:00
|
|
|
self.assertEqual(asm.split().count('RETURN_VALUE'), 2)
|
|
|
|
|
|
|
|
def test_elim_jump_after_return1(self):
|
|
|
|
# Eliminate dead code: jumps immediately after returns can't be reached
|
|
|
|
def f(cond1, cond2):
|
|
|
|
if cond1: return 1
|
|
|
|
if cond2: return 2
|
|
|
|
while 1:
|
|
|
|
return 3
|
|
|
|
while 1:
|
|
|
|
if cond1: return 4
|
|
|
|
return 5
|
|
|
|
return 6
|
|
|
|
asm = disassemble(f)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('JUMP_FORWARD' not in asm)
|
|
|
|
self.assertTrue('JUMP_ABSOLUTE' not in asm)
|
2006-10-14 18:33:38 -03:00
|
|
|
self.assertEqual(asm.split().count('RETURN_VALUE'), 6)
|
|
|
|
|
|
|
|
def test_elim_jump_after_return2(self):
|
|
|
|
# Eliminate dead code: jumps immediately after returns can't be reached
|
|
|
|
def f(cond1, cond2):
|
|
|
|
while 1:
|
|
|
|
if cond1: return 4
|
|
|
|
asm = disassemble(f)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('JUMP_FORWARD' not in asm)
|
2006-10-14 18:33:38 -03:00
|
|
|
# There should be one jump for the while loop.
|
|
|
|
self.assertEqual(asm.split().count('JUMP_ABSOLUTE'), 1)
|
|
|
|
self.assertEqual(asm.split().count('RETURN_VALUE'), 2)
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_main(verbose=None):
|
|
|
|
import sys
|
|
|
|
from test import test_support
|
2010-01-08 14:41:40 -04:00
|
|
|
import warnings
|
2004-08-23 20:37:48 -03:00
|
|
|
test_classes = (TestTranforms,)
|
2010-01-08 14:41:40 -04:00
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
# Silence Py3k warning
|
|
|
|
warnings.filterwarnings("ignore", "backquote not supported",
|
|
|
|
SyntaxWarning)
|
|
|
|
test_support.run_unittest(*test_classes)
|
|
|
|
|
|
|
|
# verify reference counting
|
|
|
|
if verbose and hasattr(sys, "gettotalrefcount"):
|
|
|
|
import gc
|
|
|
|
counts = [None] * 5
|
|
|
|
for i in xrange(len(counts)):
|
|
|
|
test_support.run_unittest(*test_classes)
|
|
|
|
gc.collect()
|
|
|
|
counts[i] = sys.gettotalrefcount()
|
|
|
|
print counts
|
2004-08-23 20:37:48 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|