| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 1 | import dis | 
| Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 2 | import re | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 3 | import sys | 
| Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 4 | from io import StringIO | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 5 | import unittest | 
| Mark Dickinson | 7c9e803 | 2011-03-23 17:59:37 +0000 | [diff] [blame] | 6 | from math import copysign | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 7 |  | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 8 | from test.bytecode_helper import BytecodeTestCase | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 9 |  | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 10 | class TestTranforms(BytecodeTestCase): | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 11 |  | 
 | 12 |     def test_unot(self): | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 13 |         # UNARY_NOT POP_JUMP_IF_FALSE  -->  POP_JUMP_IF_TRUE' | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 14 |         def unot(x): | 
 | 15 |             if not x == 2: | 
 | 16 |                 del x | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 17 |         self.assertNotInBytecode(unot, 'UNARY_NOT') | 
 | 18 |         self.assertNotInBytecode(unot, 'POP_JUMP_IF_FALSE') | 
 | 19 |         self.assertInBytecode(unot, 'POP_JUMP_IF_TRUE') | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 20 |  | 
 | 21 |     def test_elim_inversion_of_is_or_in(self): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 22 |         for line, cmp_op in ( | 
 | 23 |             ('not a is b', 'is not',), | 
 | 24 |             ('not a in b', 'not in',), | 
 | 25 |             ('not a is not b', 'is',), | 
 | 26 |             ('not a not in b', 'in',), | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 27 |             ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 28 |             code = compile(line, '', 'single') | 
 | 29 |             self.assertInBytecode(code, 'COMPARE_OP', cmp_op) | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 30 |  | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 31 |     def test_global_as_constant(self): | 
 | 32 |         # LOAD_GLOBAL None/True/False  -->  LOAD_CONST None/True/False | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 33 |         def f(x): | 
| Tim Peters | 66cb018 | 2004-08-26 05:23:19 +0000 | [diff] [blame] | 34 |             None | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 35 |             None | 
| Tim Peters | 66cb018 | 2004-08-26 05:23:19 +0000 | [diff] [blame] | 36 |             return x | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 37 |         def g(x): | 
 | 38 |             True | 
 | 39 |             return x | 
 | 40 |         def h(x): | 
 | 41 |             False | 
 | 42 |             return x | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 43 |         for func, elem in ((f, None), (g, True), (h, False)): | 
 | 44 |             self.assertNotInBytecode(func, 'LOAD_GLOBAL') | 
 | 45 |             self.assertInBytecode(func, 'LOAD_CONST', elem) | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 46 |         def f(): | 
 | 47 |             'Adding a docstring made this test fail in Py2.5.0' | 
 | 48 |             return None | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 49 |         self.assertNotInBytecode(f, 'LOAD_GLOBAL') | 
 | 50 |         self.assertInBytecode(f, 'LOAD_CONST', None) | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 51 |  | 
 | 52 |     def test_while_one(self): | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 53 |         # Skip over:  LOAD_CONST trueconst  POP_JUMP_IF_FALSE xx | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 54 |         def f(): | 
| Tim Peters | 66cb018 | 2004-08-26 05:23:19 +0000 | [diff] [blame] | 55 |             while 1: | 
 | 56 |                 pass | 
 | 57 |             return list | 
| Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 58 |         for elem in ('LOAD_CONST', 'POP_JUMP_IF_FALSE'): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 59 |             self.assertNotInBytecode(f, elem) | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 60 |         for elem in ('JUMP_ABSOLUTE',): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 61 |             self.assertInBytecode(f, elem) | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 62 |  | 
 | 63 |     def test_pack_unpack(self): | 
 | 64 |         for line, elem in ( | 
| Raymond Hettinger | 2c31a05 | 2004-09-22 18:44:21 +0000 | [diff] [blame] | 65 |             ('a, = a,', 'LOAD_CONST',), | 
 | 66 |             ('a, b = a, b', 'ROT_TWO',), | 
 | 67 |             ('a, b, c = a, b, c', 'ROT_THREE',), | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 68 |             ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 69 |             code = compile(line,'','single') | 
 | 70 |             self.assertInBytecode(code, elem) | 
 | 71 |             self.assertNotInBytecode(code, 'BUILD_TUPLE') | 
 | 72 |             self.assertNotInBytecode(code, 'UNPACK_TUPLE') | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 73 |  | 
| Raymond Hettinger | 2c31a05 | 2004-09-22 18:44:21 +0000 | [diff] [blame] | 74 |     def test_folding_of_tuples_of_constants(self): | 
 | 75 |         for line, elem in ( | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 76 |             ('a = 1,2,3', (1, 2, 3)), | 
 | 77 |             ('("a","b","c")', ('a', 'b', 'c')), | 
 | 78 |             ('a,b,c = 1,2,3', (1, 2, 3)), | 
 | 79 |             ('(None, 1, None)', (None, 1, None)), | 
 | 80 |             ('((1, 2), 3, 4)', ((1, 2), 3, 4)), | 
| Raymond Hettinger | 2c31a05 | 2004-09-22 18:44:21 +0000 | [diff] [blame] | 81 |             ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 82 |             code = compile(line,'','single') | 
 | 83 |             self.assertInBytecode(code, 'LOAD_CONST', elem) | 
 | 84 |             self.assertNotInBytecode(code, 'BUILD_TUPLE') | 
| Raymond Hettinger | 2c31a05 | 2004-09-22 18:44:21 +0000 | [diff] [blame] | 85 |  | 
| Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 86 |         # Long tuples should be folded too. | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 87 |         code = compile(repr(tuple(range(10000))),'','single') | 
 | 88 |         self.assertNotInBytecode(code, 'BUILD_TUPLE') | 
| Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 89 |         # One LOAD_CONST for the tuple, one for the None return value | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 90 |         load_consts = [instr for instr in dis.get_instructions(code) | 
 | 91 |                               if instr.opname == 'LOAD_CONST'] | 
 | 92 |         self.assertEqual(len(load_consts), 2) | 
| Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 93 |  | 
| Raymond Hettinger | 23109ef | 2004-10-26 08:59:14 +0000 | [diff] [blame] | 94 |         # Bug 1053819:  Tuple of constants misidentified when presented with: | 
 | 95 |         # . . . opcode_with_arg 100   unary_opcode   BUILD_TUPLE 1  . . . | 
 | 96 |         # The following would segfault upon compilation | 
 | 97 |         def crater(): | 
 | 98 |             (~[ | 
 | 99 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 100 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 101 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 102 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 103 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 104 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 105 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 106 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 107 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 108 |                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 
 | 109 |             ],) | 
 | 110 |  | 
| Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 111 |     def test_folding_of_lists_of_constants(self): | 
 | 112 |         for line, elem in ( | 
 | 113 |             # in/not in constants with BUILD_LIST should be folded to a tuple: | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 114 |             ('a in [1,2,3]', (1, 2, 3)), | 
 | 115 |             ('a not in ["a","b","c"]', ('a', 'b', 'c')), | 
 | 116 |             ('a in [None, 1, None]', (None, 1, None)), | 
 | 117 |             ('a not in [(1, 2), 3, 4]', ((1, 2), 3, 4)), | 
| Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 118 |             ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 119 |             code = compile(line, '', 'single') | 
 | 120 |             self.assertInBytecode(code, 'LOAD_CONST', elem) | 
 | 121 |             self.assertNotInBytecode(code, 'BUILD_LIST') | 
| Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 122 |  | 
 | 123 |     def test_folding_of_sets_of_constants(self): | 
 | 124 |         for line, elem in ( | 
 | 125 |             # in/not in constants with BUILD_SET should be folded to a frozenset: | 
 | 126 |             ('a in {1,2,3}', frozenset({1, 2, 3})), | 
 | 127 |             ('a not in {"a","b","c"}', frozenset({'a', 'c', 'b'})), | 
 | 128 |             ('a in {None, 1, None}', frozenset({1, None})), | 
 | 129 |             ('a not in {(1, 2), 3, 4}', frozenset({(1, 2), 3, 4})), | 
 | 130 |             ('a in {1, 2, 3, 3, 2, 1}', frozenset({1, 2, 3})), | 
 | 131 |             ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 132 |             code = compile(line, '', 'single') | 
 | 133 |             self.assertNotInBytecode(code, 'BUILD_SET') | 
 | 134 |             self.assertInBytecode(code, 'LOAD_CONST', elem) | 
| Antoine Pitrou | b7fbcd3 | 2010-01-16 18:37:38 +0000 | [diff] [blame] | 135 |  | 
 | 136 |         # Ensure that the resulting code actually works: | 
 | 137 |         def f(a): | 
 | 138 |             return a in {1, 2, 3} | 
 | 139 |  | 
 | 140 |         def g(a): | 
 | 141 |             return a not in {1, 2, 3} | 
 | 142 |  | 
 | 143 |         self.assertTrue(f(3)) | 
 | 144 |         self.assertTrue(not f(4)) | 
 | 145 |  | 
 | 146 |         self.assertTrue(not g(3)) | 
 | 147 |         self.assertTrue(g(4)) | 
 | 148 |  | 
 | 149 |  | 
| Raymond Hettinger | c34f867 | 2005-01-02 06:17:33 +0000 | [diff] [blame] | 150 |     def test_folding_of_binops_on_constants(self): | 
 | 151 |         for line, elem in ( | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 152 |             ('a = 2+3+4', 9),                   # chained fold | 
 | 153 |             ('"@"*4', '@@@@'),                  # check string ops | 
 | 154 |             ('a="abc" + "def"', 'abcdef'),      # check string ops | 
 | 155 |             ('a = 3**4', 81),                   # binary power | 
 | 156 |             ('a = 3*4', 12),                    # binary multiply | 
 | 157 |             ('a = 13//4', 3),                   # binary floor divide | 
 | 158 |             ('a = 14%4', 2),                    # binary modulo | 
 | 159 |             ('a = 2+3', 5),                     # binary add | 
 | 160 |             ('a = 13-4', 9),                    # binary subtract | 
 | 161 |             ('a = (12,13)[1]', 13),             # binary subscr | 
 | 162 |             ('a = 13 << 2', 52),                # binary lshift | 
 | 163 |             ('a = 13 >> 2', 3),                 # binary rshift | 
 | 164 |             ('a = 13 & 7', 5),                  # binary and | 
 | 165 |             ('a = 13 ^ 7', 10),                 # binary xor | 
 | 166 |             ('a = 13 | 7', 15),                 # binary or | 
| Raymond Hettinger | c34f867 | 2005-01-02 06:17:33 +0000 | [diff] [blame] | 167 |             ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 168 |             code = compile(line, '', 'single') | 
 | 169 |             self.assertInBytecode(code, 'LOAD_CONST', elem) | 
 | 170 |             for instr in dis.get_instructions(code): | 
 | 171 |                 self.assertFalse(instr.opname.startswith('BINARY_')) | 
| Raymond Hettinger | c34f867 | 2005-01-02 06:17:33 +0000 | [diff] [blame] | 172 |  | 
 | 173 |         # Verify that unfoldables are skipped | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 174 |         code = compile('a=2+"b"', '', 'single') | 
 | 175 |         self.assertInBytecode(code, 'LOAD_CONST', 2) | 
 | 176 |         self.assertInBytecode(code, 'LOAD_CONST', 'b') | 
| Raymond Hettinger | c34f867 | 2005-01-02 06:17:33 +0000 | [diff] [blame] | 177 |  | 
| Raymond Hettinger | 9feb267 | 2005-01-26 12:50:05 +0000 | [diff] [blame] | 178 |         # Verify that large sequences do not result from folding | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 179 |         code = compile('a="x"*1000', '', 'single') | 
 | 180 |         self.assertInBytecode(code, 'LOAD_CONST', 1000) | 
| Raymond Hettinger | 9feb267 | 2005-01-26 12:50:05 +0000 | [diff] [blame] | 181 |  | 
| Ezio Melotti | 2df6a93 | 2011-04-15 16:38:34 +0300 | [diff] [blame] | 182 |     def test_binary_subscr_on_unicode(self): | 
 | 183 |         # valid code get optimized | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 184 |         code = compile('"foo"[0]', '', 'single') | 
 | 185 |         self.assertInBytecode(code, 'LOAD_CONST', 'f') | 
 | 186 |         self.assertNotInBytecode(code, 'BINARY_SUBSCR') | 
 | 187 |         code = compile('"\u0061\uffff"[1]', '', 'single') | 
 | 188 |         self.assertInBytecode(code, 'LOAD_CONST', '\uffff') | 
 | 189 |         self.assertNotInBytecode(code,'BINARY_SUBSCR') | 
 | 190 |  | 
 | 191 |         # With PEP 393, non-BMP char get optimized | 
 | 192 |         code = compile('"\U00012345"[0]', '', 'single') | 
 | 193 |         self.assertInBytecode(code, 'LOAD_CONST', '\U00012345') | 
 | 194 |         self.assertNotInBytecode(code, 'BINARY_SUBSCR') | 
| Ezio Melotti | 2df6a93 | 2011-04-15 16:38:34 +0300 | [diff] [blame] | 195 |  | 
 | 196 |         # invalid code doesn't get optimized | 
 | 197 |         # out of range | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 198 |         code = compile('"fuu"[10]', '', 'single') | 
 | 199 |         self.assertInBytecode(code, 'BINARY_SUBSCR') | 
| Ezio Melotti | 2df6a93 | 2011-04-15 16:38:34 +0300 | [diff] [blame] | 200 |  | 
| Raymond Hettinger | afd842f | 2005-02-20 12:46:54 +0000 | [diff] [blame] | 201 |     def test_folding_of_unaryops_on_constants(self): | 
 | 202 |         for line, elem in ( | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 203 |             ('-0.5', -0.5),                     # unary negative | 
 | 204 |             ('-0.0', -0.0),                     # -0.0 | 
 | 205 |             ('-(1.0-1.0)', -0.0),               # -0.0 after folding | 
 | 206 |             ('-0', 0),                          # -0 | 
 | 207 |             ('~-2', 1),                         # unary invert | 
 | 208 |             ('+1', 1),                          # unary positive | 
| Raymond Hettinger | afd842f | 2005-02-20 12:46:54 +0000 | [diff] [blame] | 209 |         ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 210 |             code = compile(line, '', 'single') | 
 | 211 |             self.assertInBytecode(code, 'LOAD_CONST', elem) | 
 | 212 |             for instr in dis.get_instructions(code): | 
 | 213 |                 self.assertFalse(instr.opname.startswith('UNARY_')) | 
| Raymond Hettinger | afd842f | 2005-02-20 12:46:54 +0000 | [diff] [blame] | 214 |  | 
| Mark Dickinson | 7c9e803 | 2011-03-23 17:59:37 +0000 | [diff] [blame] | 215 |         # Check that -0.0 works after marshaling | 
 | 216 |         def negzero(): | 
 | 217 |             return -(1.0-1.0) | 
 | 218 |  | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 219 |         for instr in dis.get_instructions(code): | 
 | 220 |             self.assertFalse(instr.opname.startswith('UNARY_')) | 
| Mark Dickinson | 7c9e803 | 2011-03-23 17:59:37 +0000 | [diff] [blame] | 221 |  | 
| Raymond Hettinger | afd842f | 2005-02-20 12:46:54 +0000 | [diff] [blame] | 222 |         # Verify that unfoldables are skipped | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 223 |         for line, elem, opname in ( | 
 | 224 |             ('-"abc"', 'abc', 'UNARY_NEGATIVE'), | 
 | 225 |             ('~"abc"', 'abc', 'UNARY_INVERT'), | 
| Raymond Hettinger | afd842f | 2005-02-20 12:46:54 +0000 | [diff] [blame] | 226 |         ): | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 227 |             code = compile(line, '', 'single') | 
 | 228 |             self.assertInBytecode(code, 'LOAD_CONST', elem) | 
 | 229 |             self.assertInBytecode(code, opname) | 
| Raymond Hettinger | afd842f | 2005-02-20 12:46:54 +0000 | [diff] [blame] | 230 |  | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 231 |     def test_elim_extra_return(self): | 
 | 232 |         # RETURN LOAD_CONST None RETURN  -->  RETURN | 
 | 233 |         def f(x): | 
 | 234 |             return x | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 235 |         self.assertNotInBytecode(f, 'LOAD_CONST', None) | 
 | 236 |         returns = [instr for instr in dis.get_instructions(f) | 
 | 237 |                           if instr.opname == 'RETURN_VALUE'] | 
 | 238 |         self.assertEqual(len(returns), 1) | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 239 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 240 |     def test_elim_jump_to_return(self): | 
 | 241 |         # JUMP_FORWARD to RETURN -->  RETURN | 
 | 242 |         def f(cond, true_value, false_value): | 
 | 243 |             return true_value if cond else false_value | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 244 |         self.assertNotInBytecode(f, 'JUMP_FORWARD') | 
 | 245 |         self.assertNotInBytecode(f, 'JUMP_ABSOLUTE') | 
 | 246 |         returns = [instr for instr in dis.get_instructions(f) | 
 | 247 |                           if instr.opname == 'RETURN_VALUE'] | 
 | 248 |         self.assertEqual(len(returns), 2) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 249 |  | 
 | 250 |     def test_elim_jump_after_return1(self): | 
 | 251 |         # Eliminate dead code: jumps immediately after returns can't be reached | 
 | 252 |         def f(cond1, cond2): | 
 | 253 |             if cond1: return 1 | 
 | 254 |             if cond2: return 2 | 
 | 255 |             while 1: | 
 | 256 |                 return 3 | 
 | 257 |             while 1: | 
 | 258 |                 if cond1: return 4 | 
 | 259 |                 return 5 | 
 | 260 |             return 6 | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 261 |         self.assertNotInBytecode(f, 'JUMP_FORWARD') | 
 | 262 |         self.assertNotInBytecode(f, 'JUMP_ABSOLUTE') | 
 | 263 |         returns = [instr for instr in dis.get_instructions(f) | 
 | 264 |                           if instr.opname == 'RETURN_VALUE'] | 
 | 265 |         self.assertEqual(len(returns), 6) | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 266 |  | 
 | 267 |     def test_elim_jump_after_return2(self): | 
 | 268 |         # Eliminate dead code: jumps immediately after returns can't be reached | 
 | 269 |         def f(cond1, cond2): | 
 | 270 |             while 1: | 
 | 271 |                 if cond1: return 4 | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 272 |         self.assertNotInBytecode(f, 'JUMP_FORWARD') | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 273 |         # There should be one jump for the while loop. | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 274 |         returns = [instr for instr in dis.get_instructions(f) | 
 | 275 |                           if instr.opname == 'JUMP_ABSOLUTE'] | 
 | 276 |         self.assertEqual(len(returns), 1) | 
 | 277 |         returns = [instr for instr in dis.get_instructions(f) | 
 | 278 |                           if instr.opname == 'RETURN_VALUE'] | 
 | 279 |         self.assertEqual(len(returns), 2) | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 280 |  | 
| Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 281 |     def test_make_function_doesnt_bail(self): | 
 | 282 |         def f(): | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 283 |             def g()->1+1: | 
| Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 284 |                 pass | 
 | 285 |             return g | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 286 |         self.assertNotInBytecode(f, 'BINARY_ADD') | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 287 |  | 
| Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 288 |     def test_constant_folding(self): | 
 | 289 |         # Issue #11244: aggressive constant folding. | 
 | 290 |         exprs = [ | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 291 |             '3 * -5', | 
 | 292 |             '-3 * 5', | 
 | 293 |             '2 * (3 * 4)', | 
 | 294 |             '(2 * 3) * 4', | 
 | 295 |             '(-1, 2, 3)', | 
 | 296 |             '(1, -2, 3)', | 
 | 297 |             '(1, 2, -3)', | 
 | 298 |             '(1, 2, -3) * 6', | 
 | 299 |             'lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}', | 
| Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 300 |         ] | 
 | 301 |         for e in exprs: | 
| Nick Coghlan | d624517 | 2013-05-07 00:03:00 +1000 | [diff] [blame] | 302 |             code = compile(e, '', 'single') | 
 | 303 |             for instr in dis.get_instructions(code): | 
 | 304 |                 self.assertFalse(instr.opname.startswith('UNARY_')) | 
 | 305 |                 self.assertFalse(instr.opname.startswith('BINARY_')) | 
 | 306 |                 self.assertFalse(instr.opname.startswith('BUILD_')) | 
 | 307 |  | 
| Antoine Pitrou | 17b880a | 2011-03-11 17:27:02 +0100 | [diff] [blame] | 308 |  | 
| Raymond Hettinger | 0661e91 | 2011-03-15 15:03:36 -0700 | [diff] [blame] | 309 | class TestBuglets(unittest.TestCase): | 
 | 310 |  | 
| Raymond Hettinger | 5bd75b8 | 2011-03-15 15:07:38 -0700 | [diff] [blame] | 311 |     def test_bug_11510(self): | 
 | 312 |         # folded constant set optimization was commingled with the tuple | 
 | 313 |         # unpacking optimization which would fail if the set had duplicate | 
 | 314 |         # elements so that the set length was unexpected | 
 | 315 |         def f(): | 
 | 316 |             x, y = {1, 1} | 
 | 317 |             return x, y | 
 | 318 |         with self.assertRaises(ValueError): | 
 | 319 |             f() | 
| Raymond Hettinger | 0661e91 | 2011-03-15 15:03:36 -0700 | [diff] [blame] | 320 |  | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 321 |  | 
| Raymond Hettinger | fd2d1f7 | 2004-08-23 23:37:48 +0000 | [diff] [blame] | 322 | if __name__ == "__main__": | 
| Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 323 |     unittest.main() |