aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_peepholer.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2021-09-10 17:29:21 +0100
committerGitHub <noreply@github.com>2021-09-10 18:29:21 +0200
commitf8d624d6a571d6e773f53ad80aa93face7e8395b (patch)
tree0bdcf26da275bb8c9a485fe8587e9242b125fc6b /Lib/test/test_peepholer.py
parente86bcfa58080f152f242c756f625f4015671f168 (diff)
downloadcpython-f8d624d6a571d6e773f53ad80aa93face7e8395b.tar.gz
cpython-f8d624d6a571d6e773f53ad80aa93face7e8395b.zip
bpo-45144: use subTests in test_peepholer (GH-28247)
Diffstat (limited to 'Lib/test/test_peepholer.py')
-rw-r--r--Lib/test/test_peepholer.py96
1 files changed, 53 insertions, 43 deletions
diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py
index bfafc3ee4b9..9478c47f7c3 100644
--- a/Lib/test/test_peepholer.py
+++ b/Lib/test/test_peepholer.py
@@ -72,9 +72,10 @@ class TestTranforms(BytecodeTestCase):
('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 @@ class TestTranforms(BytecodeTestCase):
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 @@ class TestTranforms(BytecodeTestCase):
('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 @@ class TestTranforms(BytecodeTestCase):
('(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 @@ class TestTranforms(BytecodeTestCase):
('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 @@ class TestTranforms(BytecodeTestCase):
('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 @@ class TestTranforms(BytecodeTestCase):
('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 @@ class TestTranforms(BytecodeTestCase):
('~-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 @@ class TestTranforms(BytecodeTestCase):
('-"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 @@ class TestTranforms(BytecodeTestCase):
'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():