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