diff options
Diffstat (limited to 'Lib/test/test_peepholer.py')
| -rw-r--r-- | Lib/test/test_peepholer.py | 47 | 
1 files changed, 41 insertions, 6 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 53719d3a82..1e782cfd6d 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -3,13 +3,16 @@ import re  import sys  from io import StringIO  import unittest +from math import copysign  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 @@ -17,6 +20,7 @@ def disassemble(func):  def dis_single(line):      return disassemble(compile(line, '', 'single')) +  class TestTranforms(unittest.TestCase):      def test_unot(self): @@ -99,6 +103,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 @@ -208,14 +218,13 @@ class TestTranforms(unittest.TestCase):          # out of range          asm = dis_single('"fuu"[10]')          self.assertIn('BINARY_SUBSCR', asm) -        # non-BMP char (see #5057) -        asm = dis_single('"\U00012345"[0]') -        self.assertIn('BINARY_SUBSCR', asm) -      def test_folding_of_unaryops_on_constants(self):          for line, elem in (              ('-0.5', '(-0.5)'),                     # unary negative +            ('-0.0', '(-0.0)'),                     # -0.0 +            ('-(1.0-1.0)','(-0.0)'),                # -0.0 after folding +            ('-0', '(0)'),                          # -0              ('~-2', '(1)'),                         # unary invert              ('+1', '(1)'),                          # unary positive          ): @@ -223,6 +232,13 @@ class TestTranforms(unittest.TestCase):              self.assertIn(elem, asm, asm)              self.assertNotIn('UNARY_', asm) +        # Check that -0.0 works after marshaling +        def negzero(): +            return -(1.0-1.0) + +        self.assertNotIn('UNARY_', disassemble(negzero)) +        self.assertTrue(copysign(1.0, negzero()) < 0) +          # Verify that unfoldables are skipped          for line, elem in (              ('-"abc"', "('abc')"),                  # unary negative @@ -285,6 +301,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) +  class TestBuglets(unittest.TestCase):      def test_bug_11510(self):  | 
