@@ -72,9 +72,10 @@ def test_elim_inversion_of_is_or_in(self):
72
72
('not a in b' , 'CONTAINS_OP' , 1 ,),
73
73
('not a not in b' , 'CONTAINS_OP' , 0 ,),
74
74
):
75
- code = compile (line , '' , 'single' )
76
- self .assertInBytecode (code , cmp_op , invert )
77
- self .check_lnotab (code )
75
+ with self .subTest (line = line ):
76
+ code = compile (line , '' , 'single' )
77
+ self .assertInBytecode (code , cmp_op , invert )
78
+ self .check_lnotab (code )
78
79
79
80
def test_global_as_constant (self ):
80
81
# LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False
@@ -90,9 +91,10 @@ def h():
90
91
return x
91
92
92
93
for func , elem in ((f , None ), (g , True ), (h , False )):
93
- self .assertNotInBytecode (func , 'LOAD_GLOBAL' )
94
- self .assertInBytecode (func , 'LOAD_CONST' , elem )
95
- self .check_lnotab (func )
94
+ with self .subTest (func = func ):
95
+ self .assertNotInBytecode (func , 'LOAD_GLOBAL' )
96
+ self .assertInBytecode (func , 'LOAD_CONST' , elem )
97
+ self .check_lnotab (func )
96
98
97
99
def f ():
98
100
'Adding a docstring made this test fail in Py2.5.0'
@@ -120,11 +122,12 @@ def test_pack_unpack(self):
120
122
('a, b = a, b' , 'ROT_TWO' ,),
121
123
('a, b, c = a, b, c' , 'ROT_THREE' ,),
122
124
):
123
- code = compile (line ,'' ,'single' )
124
- self .assertInBytecode (code , elem )
125
- self .assertNotInBytecode (code , 'BUILD_TUPLE' )
126
- self .assertNotInBytecode (code , 'UNPACK_TUPLE' )
127
- self .check_lnotab (code )
125
+ with self .subTest (line = line ):
126
+ code = compile (line ,'' ,'single' )
127
+ self .assertInBytecode (code , elem )
128
+ self .assertNotInBytecode (code , 'BUILD_TUPLE' )
129
+ self .assertNotInBytecode (code , 'UNPACK_TUPLE' )
130
+ self .check_lnotab (code )
128
131
129
132
def test_folding_of_tuples_of_constants (self ):
130
133
for line , elem in (
@@ -134,10 +137,11 @@ def test_folding_of_tuples_of_constants(self):
134
137
('(None, 1, None)' , (None , 1 , None )),
135
138
('((1, 2), 3, 4)' , ((1 , 2 ), 3 , 4 )),
136
139
):
137
- code = compile (line ,'' ,'single' )
138
- self .assertInBytecode (code , 'LOAD_CONST' , elem )
139
- self .assertNotInBytecode (code , 'BUILD_TUPLE' )
140
- self .check_lnotab (code )
140
+ with self .subTest (line = line ):
141
+ code = compile (line ,'' ,'single' )
142
+ self .assertInBytecode (code , 'LOAD_CONST' , elem )
143
+ self .assertNotInBytecode (code , 'BUILD_TUPLE' )
144
+ self .check_lnotab (code )
141
145
142
146
# Long tuples should be folded too.
143
147
code = compile (repr (tuple (range (10000 ))),'' ,'single' )
@@ -174,10 +178,11 @@ def test_folding_of_lists_of_constants(self):
174
178
('a in [None, 1, None]' , (None , 1 , None )),
175
179
('a not in [(1, 2), 3, 4]' , ((1 , 2 ), 3 , 4 )),
176
180
):
177
- code = compile (line , '' , 'single' )
178
- self .assertInBytecode (code , 'LOAD_CONST' , elem )
179
- self .assertNotInBytecode (code , 'BUILD_LIST' )
180
- self .check_lnotab (code )
181
+ with self .subTest (line = line ):
182
+ code = compile (line , '' , 'single' )
183
+ self .assertInBytecode (code , 'LOAD_CONST' , elem )
184
+ self .assertNotInBytecode (code , 'BUILD_LIST' )
185
+ self .check_lnotab (code )
181
186
182
187
def test_folding_of_sets_of_constants (self ):
183
188
for line , elem in (
@@ -188,10 +193,11 @@ def test_folding_of_sets_of_constants(self):
188
193
('a not in {(1, 2), 3, 4}' , frozenset ({(1 , 2 ), 3 , 4 })),
189
194
('a in {1, 2, 3, 3, 2, 1}' , frozenset ({1 , 2 , 3 })),
190
195
):
191
- code = compile (line , '' , 'single' )
192
- self .assertNotInBytecode (code , 'BUILD_SET' )
193
- self .assertInBytecode (code , 'LOAD_CONST' , elem )
194
- self .check_lnotab (code )
196
+ with self .subTest (line = line ):
197
+ code = compile (line , '' , 'single' )
198
+ self .assertNotInBytecode (code , 'BUILD_SET' )
199
+ self .assertInBytecode (code , 'LOAD_CONST' , elem )
200
+ self .check_lnotab (code )
195
201
196
202
# Ensure that the resulting code actually works:
197
203
def f (a ):
@@ -227,11 +233,12 @@ def test_folding_of_binops_on_constants(self):
227
233
('a = 13 ^ 7' , 10 ), # binary xor
228
234
('a = 13 | 7' , 15 ), # binary or
229
235
):
230
- code = compile (line , '' , 'single' )
231
- self .assertInBytecode (code , 'LOAD_CONST' , elem )
232
- for instr in dis .get_instructions (code ):
233
- self .assertFalse (instr .opname .startswith ('BINARY_' ))
234
- self .check_lnotab (code )
236
+ with self .subTest (line = line ):
237
+ code = compile (line , '' , 'single' )
238
+ self .assertInBytecode (code , 'LOAD_CONST' , elem )
239
+ for instr in dis .get_instructions (code ):
240
+ self .assertFalse (instr .opname .startswith ('BINARY_' ))
241
+ self .check_lnotab (code )
235
242
236
243
# Verify that unfoldables are skipped
237
244
code = compile ('a=2+"b"' , '' , 'single' )
@@ -285,11 +292,12 @@ def test_folding_of_unaryops_on_constants(self):
285
292
('~-2' , 1 ), # unary invert
286
293
('+1' , 1 ), # unary positive
287
294
):
288
- code = compile (line , '' , 'single' )
289
- self .assertInBytecode (code , 'LOAD_CONST' , elem )
290
- for instr in dis .get_instructions (code ):
291
- self .assertFalse (instr .opname .startswith ('UNARY_' ))
292
- self .check_lnotab (code )
295
+ with self .subTest (line = line ):
296
+ code = compile (line , '' , 'single' )
297
+ self .assertInBytecode (code , 'LOAD_CONST' , elem )
298
+ for instr in dis .get_instructions (code ):
299
+ self .assertFalse (instr .opname .startswith ('UNARY_' ))
300
+ self .check_lnotab (code )
293
301
294
302
# Check that -0.0 works after marshaling
295
303
def negzero ():
@@ -304,10 +312,11 @@ def negzero():
304
312
('-"abc"' , 'abc' , 'UNARY_NEGATIVE' ),
305
313
('~"abc"' , 'abc' , 'UNARY_INVERT' ),
306
314
):
307
- code = compile (line , '' , 'single' )
308
- self .assertInBytecode (code , 'LOAD_CONST' , elem )
309
- self .assertInBytecode (code , opname )
310
- self .check_lnotab (code )
315
+ with self .subTest (line = line ):
316
+ code = compile (line , '' , 'single' )
317
+ self .assertInBytecode (code , 'LOAD_CONST' , elem )
318
+ self .assertInBytecode (code , opname )
319
+ self .check_lnotab (code )
311
320
312
321
def test_elim_extra_return (self ):
313
322
# RETURN LOAD_CONST None RETURN --> RETURN
@@ -432,12 +441,13 @@ def test_constant_folding(self):
432
441
'lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}' ,
433
442
]
434
443
for e in exprs :
435
- code = compile (e , '' , 'single' )
436
- for instr in dis .get_instructions (code ):
437
- self .assertFalse (instr .opname .startswith ('UNARY_' ))
438
- self .assertFalse (instr .opname .startswith ('BINARY_' ))
439
- self .assertFalse (instr .opname .startswith ('BUILD_' ))
440
- self .check_lnotab (code )
444
+ with self .subTest (e = e ):
445
+ code = compile (e , '' , 'single' )
446
+ for instr in dis .get_instructions (code ):
447
+ self .assertFalse (instr .opname .startswith ('UNARY_' ))
448
+ self .assertFalse (instr .opname .startswith ('BINARY_' ))
449
+ self .assertFalse (instr .opname .startswith ('BUILD_' ))
450
+ self .check_lnotab (code )
441
451
442
452
def test_in_literal_list (self ):
443
453
def containtest ():
0 commit comments