mirror of
https://github.com/python/cpython
synced 2024-09-20 11:51:36 +00:00
bpo-45144: use subTests in test_peepholer (GH-28247)
This commit is contained in:
parent
e86bcfa580
commit
f8d624d6a5
|
@ -72,9 +72,10 @@ def test_elim_inversion_of_is_or_in(self):
|
|||
('not a in b', 'CONTAINS_OP', 1,),
|
||||
('not a not in b', 'CONTAINS_OP', 0,),
|
||||
):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, cmp_op, invert)
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, cmp_op, invert)
|
||||
self.check_lnotab(code)
|
||||
|
||||
def test_global_as_constant(self):
|
||||
# LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False
|
||||
|
@ -90,9 +91,10 @@ def h():
|
|||
return x
|
||||
|
||||
for func, elem in ((f, None), (g, True), (h, False)):
|
||||
self.assertNotInBytecode(func, 'LOAD_GLOBAL')
|
||||
self.assertInBytecode(func, 'LOAD_CONST', elem)
|
||||
self.check_lnotab(func)
|
||||
with self.subTest(func=func):
|
||||
self.assertNotInBytecode(func, 'LOAD_GLOBAL')
|
||||
self.assertInBytecode(func, 'LOAD_CONST', elem)
|
||||
self.check_lnotab(func)
|
||||
|
||||
def f():
|
||||
'Adding a docstring made this test fail in Py2.5.0'
|
||||
|
@ -120,11 +122,12 @@ def test_pack_unpack(self):
|
|||
('a, b = a, b', 'ROT_TWO',),
|
||||
('a, b, c = a, b, c', 'ROT_THREE',),
|
||||
):
|
||||
code = compile(line,'','single')
|
||||
self.assertInBytecode(code, elem)
|
||||
self.assertNotInBytecode(code, 'BUILD_TUPLE')
|
||||
self.assertNotInBytecode(code, 'UNPACK_TUPLE')
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line,'','single')
|
||||
self.assertInBytecode(code, elem)
|
||||
self.assertNotInBytecode(code, 'BUILD_TUPLE')
|
||||
self.assertNotInBytecode(code, 'UNPACK_TUPLE')
|
||||
self.check_lnotab(code)
|
||||
|
||||
def test_folding_of_tuples_of_constants(self):
|
||||
for line, elem in (
|
||||
|
@ -134,10 +137,11 @@ def test_folding_of_tuples_of_constants(self):
|
|||
('(None, 1, None)', (None, 1, None)),
|
||||
('((1, 2), 3, 4)', ((1, 2), 3, 4)),
|
||||
):
|
||||
code = compile(line,'','single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.assertNotInBytecode(code, 'BUILD_TUPLE')
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line,'','single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.assertNotInBytecode(code, 'BUILD_TUPLE')
|
||||
self.check_lnotab(code)
|
||||
|
||||
# Long tuples should be folded too.
|
||||
code = compile(repr(tuple(range(10000))),'','single')
|
||||
|
@ -174,10 +178,11 @@ def test_folding_of_lists_of_constants(self):
|
|||
('a in [None, 1, None]', (None, 1, None)),
|
||||
('a not in [(1, 2), 3, 4]', ((1, 2), 3, 4)),
|
||||
):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.assertNotInBytecode(code, 'BUILD_LIST')
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.assertNotInBytecode(code, 'BUILD_LIST')
|
||||
self.check_lnotab(code)
|
||||
|
||||
def test_folding_of_sets_of_constants(self):
|
||||
for line, elem in (
|
||||
|
@ -188,10 +193,11 @@ def test_folding_of_sets_of_constants(self):
|
|||
('a not in {(1, 2), 3, 4}', frozenset({(1, 2), 3, 4})),
|
||||
('a in {1, 2, 3, 3, 2, 1}', frozenset({1, 2, 3})),
|
||||
):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertNotInBytecode(code, 'BUILD_SET')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertNotInBytecode(code, 'BUILD_SET')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.check_lnotab(code)
|
||||
|
||||
# Ensure that the resulting code actually works:
|
||||
def f(a):
|
||||
|
@ -227,11 +233,12 @@ def test_folding_of_binops_on_constants(self):
|
|||
('a = 13 ^ 7', 10), # binary xor
|
||||
('a = 13 | 7', 15), # binary or
|
||||
):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
for instr in dis.get_instructions(code):
|
||||
self.assertFalse(instr.opname.startswith('BINARY_'))
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
for instr in dis.get_instructions(code):
|
||||
self.assertFalse(instr.opname.startswith('BINARY_'))
|
||||
self.check_lnotab(code)
|
||||
|
||||
# Verify that unfoldables are skipped
|
||||
code = compile('a=2+"b"', '', 'single')
|
||||
|
@ -285,11 +292,12 @@ def test_folding_of_unaryops_on_constants(self):
|
|||
('~-2', 1), # unary invert
|
||||
('+1', 1), # unary positive
|
||||
):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
for instr in dis.get_instructions(code):
|
||||
self.assertFalse(instr.opname.startswith('UNARY_'))
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
for instr in dis.get_instructions(code):
|
||||
self.assertFalse(instr.opname.startswith('UNARY_'))
|
||||
self.check_lnotab(code)
|
||||
|
||||
# Check that -0.0 works after marshaling
|
||||
def negzero():
|
||||
|
@ -304,10 +312,11 @@ def negzero():
|
|||
('-"abc"', 'abc', 'UNARY_NEGATIVE'),
|
||||
('~"abc"', 'abc', 'UNARY_INVERT'),
|
||||
):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.assertInBytecode(code, opname)
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(line=line):
|
||||
code = compile(line, '', 'single')
|
||||
self.assertInBytecode(code, 'LOAD_CONST', elem)
|
||||
self.assertInBytecode(code, opname)
|
||||
self.check_lnotab(code)
|
||||
|
||||
def test_elim_extra_return(self):
|
||||
# RETURN LOAD_CONST None RETURN --> RETURN
|
||||
|
@ -432,12 +441,13 @@ def test_constant_folding(self):
|
|||
'lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}',
|
||||
]
|
||||
for e in exprs:
|
||||
code = compile(e, '', 'single')
|
||||
for instr in dis.get_instructions(code):
|
||||
self.assertFalse(instr.opname.startswith('UNARY_'))
|
||||
self.assertFalse(instr.opname.startswith('BINARY_'))
|
||||
self.assertFalse(instr.opname.startswith('BUILD_'))
|
||||
self.check_lnotab(code)
|
||||
with self.subTest(e=e):
|
||||
code = compile(e, '', 'single')
|
||||
for instr in dis.get_instructions(code):
|
||||
self.assertFalse(instr.opname.startswith('UNARY_'))
|
||||
self.assertFalse(instr.opname.startswith('BINARY_'))
|
||||
self.assertFalse(instr.opname.startswith('BUILD_'))
|
||||
self.check_lnotab(code)
|
||||
|
||||
def test_in_literal_list(self):
|
||||
def containtest():
|
||||
|
|
Loading…
Reference in a new issue