diff -r b7b805e05978 Lib/test/test_peepholer.py --- a/Lib/test/test_peepholer.py Wed Mar 09 19:01:04 2011 -0500 +++ b/Lib/test/test_peepholer.py Thu Mar 10 20:09:07 2011 -0500 @@ -3,6 +3,7 @@ import sys from io import StringIO import unittest +from math import copysign def disassemble(func): f = StringIO() @@ -267,6 +268,49 @@ asm = disassemble(f) self.assertNotIn('BINARY_ADD', asm) + def test_dedup_consts_after_fold(self): + def mapping(x): + # Special handling for -0.0 + if isinstance(x, float): + s = copysign(1.0, x) + elif isinstance(x, complex): + s = copysign(10.0, x.real) + copysign(1.0, x.imag) + else: + s = 0 + return type(x), x, s + + def fix(consts): + return len(consts), set(map(mapping, consts)) + + def check(f, consts): + self.assertEqual(fix(f.__code__.co_consts), fix(consts)) + + def const1(): 'doc'; return [1+2, 1+2, 1+2, 1.0+2.0] + check(const1, ('doc', 3, 3.0)) + + def const2(): return [10000+2, 10000+2, 10000+2, 10000.0+2] + check(const2, (None, 10002, 10002.0)) + + def const3(): return [1.0*0.0, -1.0*0.0, 2*0.0, -2*0.0] + check(const3, (None, 0.0, -0.0)) + + def const4(): return [1+0j, 0.5+0.5, 1+0j, 0.5+0.5, 1+0j] + check(const4, (None, 1+0j, 1.0)) + + def const5(): + return [0.0j, -0.0j, 0.0, -0.0, 1.0+0j, -(-1.0+0j), + 1.0, -(0.0-1.0j), 1j] + x = 0.0 + xj = 0j + check(const5, + (None, 0j, 0.0, 1.0, 1j, -xj, -0.0, 1+0j, -(-1+xj), -(x-1j))) + + def const6(): 'ab'; return ['a'+'b', 'a'+'b', 'a'+'b'] + check(const6, ('ab',)) + + def const7(): return [(1,2), (1,2)] + check(const7, (None, (1,2))) + def test_main(verbose=None): import sys