Skip to content

Commit f8d624d

Browse files
authored
bpo-45144: use subTests in test_peepholer (GH-28247)
1 parent e86bcfa commit f8d624d

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

Lib/test/test_peepholer.py

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ def test_elim_inversion_of_is_or_in(self):
7272
('not a in b', 'CONTAINS_OP', 1,),
7373
('not a not in b', 'CONTAINS_OP', 0,),
7474
):
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)
7879

7980
def test_global_as_constant(self):
8081
# LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False
@@ -90,9 +91,10 @@ def h():
9091
return x
9192

9293
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)
9698

9799
def f():
98100
'Adding a docstring made this test fail in Py2.5.0'
@@ -120,11 +122,12 @@ def test_pack_unpack(self):
120122
('a, b = a, b', 'ROT_TWO',),
121123
('a, b, c = a, b, c', 'ROT_THREE',),
122124
):
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)
128131

129132
def test_folding_of_tuples_of_constants(self):
130133
for line, elem in (
@@ -134,10 +137,11 @@ def test_folding_of_tuples_of_constants(self):
134137
('(None, 1, None)', (None, 1, None)),
135138
('((1, 2), 3, 4)', ((1, 2), 3, 4)),
136139
):
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)
141145

142146
# Long tuples should be folded too.
143147
code = compile(repr(tuple(range(10000))),'','single')
@@ -174,10 +178,11 @@ def test_folding_of_lists_of_constants(self):
174178
('a in [None, 1, None]', (None, 1, None)),
175179
('a not in [(1, 2), 3, 4]', ((1, 2), 3, 4)),
176180
):
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)
181186

182187
def test_folding_of_sets_of_constants(self):
183188
for line, elem in (
@@ -188,10 +193,11 @@ def test_folding_of_sets_of_constants(self):
188193
('a not in {(1, 2), 3, 4}', frozenset({(1, 2), 3, 4})),
189194
('a in {1, 2, 3, 3, 2, 1}', frozenset({1, 2, 3})),
190195
):
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)
195201

196202
# Ensure that the resulting code actually works:
197203
def f(a):
@@ -227,11 +233,12 @@ def test_folding_of_binops_on_constants(self):
227233
('a = 13 ^ 7', 10), # binary xor
228234
('a = 13 | 7', 15), # binary or
229235
):
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)
235242

236243
# Verify that unfoldables are skipped
237244
code = compile('a=2+"b"', '', 'single')
@@ -285,11 +292,12 @@ def test_folding_of_unaryops_on_constants(self):
285292
('~-2', 1), # unary invert
286293
('+1', 1), # unary positive
287294
):
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)
293301

294302
# Check that -0.0 works after marshaling
295303
def negzero():
@@ -304,10 +312,11 @@ def negzero():
304312
('-"abc"', 'abc', 'UNARY_NEGATIVE'),
305313
('~"abc"', 'abc', 'UNARY_INVERT'),
306314
):
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)
311320

312321
def test_elim_extra_return(self):
313322
# RETURN LOAD_CONST None RETURN --> RETURN
@@ -432,12 +441,13 @@ def test_constant_folding(self):
432441
'lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}',
433442
]
434443
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)
441451

442452
def test_in_literal_list(self):
443453
def containtest():

0 commit comments

Comments
 (0)