Back to index

python3.2  3.2.2
Public Member Functions
test.test_peepholer.TestTranforms Class Reference

List of all members.

Public Member Functions

def test_unot
def test_elim_inversion_of_is_or_in
def test_global_as_constant
def test_while_one
def test_pack_unpack
def test_folding_of_tuples_of_constants
def test_folding_of_lists_of_constants
def test_folding_of_sets_of_constants
def test_folding_of_binops_on_constants
def test_binary_subscr_on_unicode
def test_folding_of_unaryops_on_constants
def test_elim_extra_return
def test_elim_jump_to_return
def test_elim_jump_after_return1
def test_elim_jump_after_return2
def test_make_function_doesnt_bail

Detailed Description

Definition at line 20 of file test_peepholer.py.


Member Function Documentation

Definition at line 198 of file test_peepholer.py.

00198 
00199     def test_binary_subscr_on_unicode(self):
00200         # valid code get optimized
00201         asm = dis_single('"foo"[0]')
00202         self.assertIn("('f')", asm)
00203         self.assertNotIn('BINARY_SUBSCR', asm)
00204         asm = dis_single('"\u0061\uffff"[1]')
00205         self.assertIn("('\\uffff')", asm)
00206         self.assertNotIn('BINARY_SUBSCR', asm)
00207 
00208         # invalid code doesn't get optimized
00209         # out of range
00210         asm = dis_single('"fuu"[10]')
00211         self.assertIn('BINARY_SUBSCR', asm)
00212         # non-BMP char (see #5057)
00213         asm = dis_single('"\U00012345"[0]')
00214         self.assertIn('BINARY_SUBSCR', asm)
00215 

Here is the call graph for this function:

Definition at line 235 of file test_peepholer.py.

00235 
00236     def test_elim_extra_return(self):
00237         # RETURN LOAD_CONST None RETURN  -->  RETURN
00238         def f(x):
00239             return x
00240         asm = disassemble(f)
00241         self.assertNotIn('LOAD_CONST', asm)
00242         self.assertNotIn('(None)', asm)
00243         self.assertEqual(asm.split().count('RETURN_VALUE'), 1)

Here is the call graph for this function:

Definition at line 33 of file test_peepholer.py.

00033 
00034     def test_elim_inversion_of_is_or_in(self):
00035         for line, elem in (
00036             ('not a is b', '(is not)',),
00037             ('not a in b', '(not in)',),
00038             ('not a is not b', '(is)',),
00039             ('not a not in b', '(in)',),
00040             ):
00041             asm = dis_single(line)
00042             self.assertIn(elem, asm)

Here is the call graph for this function:

Definition at line 253 of file test_peepholer.py.

00253 
00254     def test_elim_jump_after_return1(self):
00255         # Eliminate dead code: jumps immediately after returns can't be reached
00256         def f(cond1, cond2):
00257             if cond1: return 1
00258             if cond2: return 2
00259             while 1:
00260                 return 3
00261             while 1:
00262                 if cond1: return 4
00263                 return 5
00264             return 6
00265         asm = disassemble(f)
00266         self.assertNotIn('JUMP_FORWARD', asm)
00267         self.assertNotIn('JUMP_ABSOLUTE', asm)
00268         self.assertEqual(asm.split().count('RETURN_VALUE'), 6)

Here is the call graph for this function:

Definition at line 269 of file test_peepholer.py.

00269 
00270     def test_elim_jump_after_return2(self):
00271         # Eliminate dead code: jumps immediately after returns can't be reached
00272         def f(cond1, cond2):
00273             while 1:
00274                 if cond1: return 4
00275         asm = disassemble(f)
00276         self.assertNotIn('JUMP_FORWARD', asm)
00277         # There should be one jump for the while loop.
00278         self.assertEqual(asm.split().count('JUMP_ABSOLUTE'), 1)
00279         self.assertEqual(asm.split().count('RETURN_VALUE'), 2)

Here is the call graph for this function:

Definition at line 244 of file test_peepholer.py.

00244 
00245     def test_elim_jump_to_return(self):
00246         # JUMP_FORWARD to RETURN -->  RETURN
00247         def f(cond, true_value, false_value):
00248             return true_value if cond else false_value
00249         asm = disassemble(f)
00250         self.assertNotIn('JUMP_FORWARD', asm)
00251         self.assertNotIn('JUMP_ABSOLUTE', asm)
00252         self.assertEqual(asm.split().count('RETURN_VALUE'), 2)

Here is the call graph for this function:

Definition at line 167 of file test_peepholer.py.

00167 
00168     def test_folding_of_binops_on_constants(self):
00169         for line, elem in (
00170             ('a = 2+3+4', '(9)'),                   # chained fold
00171             ('"@"*4', "('@@@@')"),                  # check string ops
00172             ('a="abc" + "def"', "('abcdef')"),      # check string ops
00173             ('a = 3**4', '(81)'),                   # binary power
00174             ('a = 3*4', '(12)'),                    # binary multiply
00175             ('a = 13//4', '(3)'),                   # binary floor divide
00176             ('a = 14%4', '(2)'),                    # binary modulo
00177             ('a = 2+3', '(5)'),                     # binary add
00178             ('a = 13-4', '(9)'),                    # binary subtract
00179             ('a = (12,13)[1]', '(13)'),             # binary subscr
00180             ('a = 13 << 2', '(52)'),                # binary lshift
00181             ('a = 13 >> 2', '(3)'),                 # binary rshift
00182             ('a = 13 & 7', '(5)'),                  # binary and
00183             ('a = 13 ^ 7', '(10)'),                 # binary xor
00184             ('a = 13 | 7', '(15)'),                 # binary or
00185             ):
00186             asm = dis_single(line)
00187             self.assertIn(elem, asm, asm)
00188             self.assertNotIn('BINARY_', asm)
00189 
00190         # Verify that unfoldables are skipped
00191         asm = dis_single('a=2+"b"')
00192         self.assertIn('(2)', asm)
00193         self.assertIn("('b')", asm)
00194 
00195         # Verify that large sequences do not result from folding
00196         asm = dis_single('a="x"*1000')
00197         self.assertIn('(1000)', asm)

Here is the call graph for this function:

Definition at line 119 of file test_peepholer.py.

00119 
00120     def test_folding_of_lists_of_constants(self):
00121         for line, elem in (
00122             # in/not in constants with BUILD_LIST should be folded to a tuple:
00123             ('a in [1,2,3]', '(1, 2, 3)'),
00124             ('a not in ["a","b","c"]', "(('a', 'b', 'c'))"),
00125             ('a in [None, 1, None]', '((None, 1, None))'),
00126             ('a not in [(1, 2), 3, 4]', '(((1, 2), 3, 4))'),
00127             ):
00128             asm = dis_single(line)
00129             self.assertIn(elem, asm)
00130             self.assertNotIn('BUILD_LIST', asm)

Here is the call graph for this function:

Definition at line 131 of file test_peepholer.py.

00131 
00132     def test_folding_of_sets_of_constants(self):
00133         for line, elem in (
00134             # in/not in constants with BUILD_SET should be folded to a frozenset:
00135             ('a in {1,2,3}', frozenset({1, 2, 3})),
00136             ('a not in {"a","b","c"}', frozenset({'a', 'c', 'b'})),
00137             ('a in {None, 1, None}', frozenset({1, None})),
00138             ('a not in {(1, 2), 3, 4}', frozenset({(1, 2), 3, 4})),
00139             ('a in {1, 2, 3, 3, 2, 1}', frozenset({1, 2, 3})),
00140             ):
00141             asm = dis_single(line)
00142             self.assertNotIn('BUILD_SET', asm)
00143 
00144             # Verify that the frozenset 'elem' is in the disassembly
00145             # The ordering of the elements in repr( frozenset ) isn't
00146             # guaranteed, so we jump through some hoops to ensure that we have
00147             # the frozenset we expect:
00148             self.assertIn('frozenset', asm)
00149             # Extract the frozenset literal from the disassembly:
00150             m = re.match(r'.*(frozenset\({.*}\)).*', asm, re.DOTALL)
00151             self.assertTrue(m)
00152             self.assertEqual(eval(m.group(1)), elem)
00153 
00154         # Ensure that the resulting code actually works:
00155         def f(a):
00156             return a in {1, 2, 3}
00157 
00158         def g(a):
00159             return a not in {1, 2, 3}
00160 
00161         self.assertTrue(f(3))
00162         self.assertTrue(not f(4))
00163 
00164         self.assertTrue(not g(3))
00165         self.assertTrue(g(4))
00166 

Here is the call graph for this function:

Definition at line 90 of file test_peepholer.py.

00090 
00091     def test_folding_of_tuples_of_constants(self):
00092         for line, elem in (
00093             ('a = 1,2,3', '((1, 2, 3))'),
00094             ('("a","b","c")', "(('a', 'b', 'c'))"),
00095             ('a,b,c = 1,2,3', '((1, 2, 3))'),
00096             ('(None, 1, None)', '((None, 1, None))'),
00097             ('((1, 2), 3, 4)', '(((1, 2), 3, 4))'),
00098             ):
00099             asm = dis_single(line)
00100             self.assertIn(elem, asm)
00101             self.assertNotIn('BUILD_TUPLE', asm)
00102 
00103         # Bug 1053819:  Tuple of constants misidentified when presented with:
00104         # . . . opcode_with_arg 100   unary_opcode   BUILD_TUPLE 1  . . .
00105         # The following would segfault upon compilation
00106         def crater():
00107             (~[
00108                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00109                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00110                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00111                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00112                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00113                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00114                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00115                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00116                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00117                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
00118             ],)

Here is the call graph for this function:

Definition at line 216 of file test_peepholer.py.

00216 
00217     def test_folding_of_unaryops_on_constants(self):
00218         for line, elem in (
00219             ('-0.5', '(-0.5)'),                     # unary negative
00220             ('~-2', '(1)'),                         # unary invert
00221             ('+1', '(1)'),                          # unary positive
00222         ):
00223             asm = dis_single(line)
00224             self.assertIn(elem, asm, asm)
00225             self.assertNotIn('UNARY_', asm)
00226 
00227         # Verify that unfoldables are skipped
00228         for line, elem in (
00229             ('-"abc"', "('abc')"),                  # unary negative
00230             ('~"abc"', "('abc')"),                  # unary invert
00231         ):
00232             asm = dis_single(line)
00233             self.assertIn(elem, asm, asm)
00234             self.assertIn('UNARY_', asm)

Here is the call graph for this function:

Definition at line 43 of file test_peepholer.py.

00043 
00044     def test_global_as_constant(self):
00045         # LOAD_GLOBAL None/True/False  -->  LOAD_CONST None/True/False
00046         def f(x):
00047             None
00048             None
00049             return x
00050         def g(x):
00051             True
00052             return x
00053         def h(x):
00054             False
00055             return x
00056         for func, name in ((f, 'None'), (g, 'True'), (h, 'False')):
00057             asm = disassemble(func)
00058             for elem in ('LOAD_GLOBAL',):
00059                 self.assertNotIn(elem, asm)
00060             for elem in ('LOAD_CONST', '('+name+')'):
00061                 self.assertIn(elem, asm)
00062         def f():
00063             'Adding a docstring made this test fail in Py2.5.0'
00064             return None
00065         self.assertIn('LOAD_CONST', disassemble(f))
00066         self.assertNotIn('LOAD_GLOBAL', disassemble(f))

Here is the call graph for this function:

Definition at line 280 of file test_peepholer.py.

00280 
00281     def test_make_function_doesnt_bail(self):
00282         def f():
00283             def g()->1+1:
00284                 pass
00285             return g
00286         asm = disassemble(f)
00287         self.assertNotIn('BINARY_ADD', asm)

Definition at line 79 of file test_peepholer.py.

00079 
00080     def test_pack_unpack(self):
00081         for line, elem in (
00082             ('a, = a,', 'LOAD_CONST',),
00083             ('a, b = a, b', 'ROT_TWO',),
00084             ('a, b, c = a, b, c', 'ROT_THREE',),
00085             ):
00086             asm = dis_single(line)
00087             self.assertIn(elem, asm)
00088             self.assertNotIn('BUILD_TUPLE', asm)
00089             self.assertNotIn('UNPACK_TUPLE', asm)

Here is the call graph for this function:

Definition at line 22 of file test_peepholer.py.

00022 
00023     def test_unot(self):
00024         # UNARY_NOT POP_JUMP_IF_FALSE  -->  POP_JUMP_IF_TRUE'
00025         def unot(x):
00026             if not x == 2:
00027                 del x
00028         asm = disassemble(unot)
00029         for elem in ('UNARY_NOT', 'POP_JUMP_IF_FALSE'):
00030             self.assertNotIn(elem, asm)
00031         for elem in ('POP_JUMP_IF_TRUE',):
00032             self.assertIn(elem, asm)

Here is the call graph for this function:

Definition at line 67 of file test_peepholer.py.

00067 
00068     def test_while_one(self):
00069         # Skip over:  LOAD_CONST trueconst  POP_JUMP_IF_FALSE xx
00070         def f():
00071             while 1:
00072                 pass
00073             return list
00074         asm = disassemble(f)
00075         for elem in ('LOAD_CONST', 'POP_JUMP_IF_FALSE'):
00076             self.assertNotIn(elem, asm)
00077         for elem in ('JUMP_ABSOLUTE',):
00078             self.assertIn(elem, asm)

Here is the call graph for this function:


The documentation for this class was generated from the following file: