gh-101951: use textwrap.dedent in compiler tests to make them more readable (GH-101950)

Fixes #101951.

Automerge-Triggered-By: GH:iritkatriel
This commit is contained in:
Irit Katriel 2023-02-16 12:31:59 +00:00 committed by GitHub
parent df7ccf6138
commit 36b139af63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -115,8 +115,8 @@ class TestSpecifics(unittest.TestCase):
repeat = 2000 repeat = 2000
longexpr = 'x = x or ' + '-x' * repeat longexpr = 'x = x or ' + '-x' * repeat
g = {} g = {}
code = ''' code = textwrap.dedent('''
def f(x): def f(x):
%s %s
%s %s
%s %s
@ -132,7 +132,7 @@ def f(x):
x -= 1 x -= 1
# EXTENDED_ARG/JUMP_ABSOLUTE here # EXTENDED_ARG/JUMP_ABSOLUTE here
return x return x
''' % ((longexpr,)*10) ''' % ((longexpr,)*10))
exec(code, g) exec(code, g)
self.assertEqual(g['f'](5), 0) self.assertEqual(g['f'](5), 0)
@ -148,10 +148,11 @@ def f(x):
def test_indentation(self): def test_indentation(self):
# testing compile() of indented block w/o trailing newline" # testing compile() of indented block w/o trailing newline"
s = """ s = textwrap.dedent("""
if 1: if 1:
if 2: if 2:
pass""" pass
""")
compile(s, "<string>", "exec") compile(s, "<string>", "exec")
# This test is probably specific to CPython and may not generalize # This test is probably specific to CPython and may not generalize
@ -1157,14 +1158,15 @@ if 1:
def test_multi_line_lambda_as_argument(self): def test_multi_line_lambda_as_argument(self):
# See gh-101928 # See gh-101928
compile(""" code = textwrap.dedent("""
def foo(param, lambda_exp): def foo(param, lambda_exp):
pass pass
foo(param=0, foo(param=0,
lambda_exp=lambda: lambda_exp=lambda:
1) 1)
""", "<test>", "exec") """)
compile(code, "<test>", "exec")
@requires_debug_ranges() @requires_debug_ranges()
@ -1252,24 +1254,24 @@ class TestSourcePositions(unittest.TestCase):
column=2, end_column=9, occurrence=2) column=2, end_column=9, occurrence=2)
def test_multiline_expression(self): def test_multiline_expression(self):
snippet = """\ snippet = textwrap.dedent("""\
f( f(
1, 2, 3, 4 1, 2, 3, 4
) )
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL', self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
line=1, end_line=3, column=0, end_column=1) line=1, end_line=3, column=0, end_column=1)
@requires_specialization @requires_specialization
def test_multiline_boolean_expression(self): def test_multiline_boolean_expression(self):
snippet = """\ snippet = textwrap.dedent("""\
if (a or if (a or
(b and not c) or (b and not c) or
not ( not (
d > 0)): d > 0)):
x = 42 x = 42
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
# jump if a is true: # jump if a is true:
self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE', self.assertOpcodeSourcePositionIs(compiled_code, 'POP_JUMP_IF_TRUE',
@ -1288,11 +1290,11 @@ if (a or
line=4, end_line=4, column=8, end_column=13, occurrence=2) line=4, end_line=4, column=8, end_column=13, occurrence=2)
def test_multiline_assert(self): def test_multiline_assert(self):
snippet = """\ snippet = textwrap.dedent("""\
assert (a > 0 and assert (a > 0 and
bb > 0 and bb > 0 and
ccc == 4), "error msg" ccc == 4), "error msg"
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_ASSERTION_ERROR', self.assertOpcodeSourcePositionIs(compiled_code, 'LOAD_ASSERTION_ERROR',
line=1, end_line=3, column=0, end_column=30, occurrence=1) line=1, end_line=3, column=0, end_column=30, occurrence=1)
@ -1305,14 +1307,14 @@ assert (a > 0 and
line=1, end_line=3, column=0, end_column=30, occurrence=1) line=1, end_line=3, column=0, end_column=30, occurrence=1)
def test_multiline_generator_expression(self): def test_multiline_generator_expression(self):
snippet = """\ snippet = textwrap.dedent("""\
((x, ((x,
2*x) 2*x)
for x for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)) and x != 50))
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
compiled_code = compiled_code.co_consts[0] compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType) self.assertIsInstance(compiled_code, types.CodeType)
@ -1324,14 +1326,14 @@ assert (a > 0 and
line=1, end_line=6, column=0, end_column=32, occurrence=1) line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_generator_expression(self): def test_multiline_async_generator_expression(self):
snippet = """\ snippet = textwrap.dedent("""\
((x, ((x,
2*x) 2*x)
async for x async for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)) and x != 50))
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
compiled_code = compiled_code.co_consts[0] compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType) self.assertIsInstance(compiled_code, types.CodeType)
@ -1341,14 +1343,14 @@ assert (a > 0 and
line=1, end_line=6, column=0, end_column=32, occurrence=1) line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_list_comprehension(self): def test_multiline_list_comprehension(self):
snippet = """\ snippet = textwrap.dedent("""\
[(x, [(x,
2*x) 2*x)
for x for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)] and x != 50)]
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
compiled_code = compiled_code.co_consts[0] compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType) self.assertIsInstance(compiled_code, types.CodeType)
@ -1360,15 +1362,15 @@ assert (a > 0 and
line=1, end_line=6, column=0, end_column=32, occurrence=1) line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_list_comprehension(self): def test_multiline_async_list_comprehension(self):
snippet = """\ snippet = textwrap.dedent("""\
async def f(): async def f():
[(x, [(x,
2*x) 2*x)
async for x async for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)] and x != 50)]
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
g = {} g = {}
eval(compiled_code, g) eval(compiled_code, g)
@ -1382,14 +1384,14 @@ async def f():
line=2, end_line=7, column=4, end_column=36, occurrence=1) line=2, end_line=7, column=4, end_column=36, occurrence=1)
def test_multiline_set_comprehension(self): def test_multiline_set_comprehension(self):
snippet = """\ snippet = textwrap.dedent("""\
{(x, {(x,
2*x) 2*x)
for x for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)} and x != 50)}
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
compiled_code = compiled_code.co_consts[0] compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType) self.assertIsInstance(compiled_code, types.CodeType)
@ -1401,15 +1403,15 @@ async def f():
line=1, end_line=6, column=0, end_column=32, occurrence=1) line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_set_comprehension(self): def test_multiline_async_set_comprehension(self):
snippet = """\ snippet = textwrap.dedent("""\
async def f(): async def f():
{(x, {(x,
2*x) 2*x)
async for x async for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)} and x != 50)}
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
g = {} g = {}
eval(compiled_code, g) eval(compiled_code, g)
@ -1423,14 +1425,14 @@ async def f():
line=2, end_line=7, column=4, end_column=36, occurrence=1) line=2, end_line=7, column=4, end_column=36, occurrence=1)
def test_multiline_dict_comprehension(self): def test_multiline_dict_comprehension(self):
snippet = """\ snippet = textwrap.dedent("""\
{x: {x:
2*x 2*x
for x for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)} and x != 50)}
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
compiled_code = compiled_code.co_consts[0] compiled_code = compiled_code.co_consts[0]
self.assertIsInstance(compiled_code, types.CodeType) self.assertIsInstance(compiled_code, types.CodeType)
@ -1442,15 +1444,15 @@ async def f():
line=1, end_line=6, column=0, end_column=32, occurrence=1) line=1, end_line=6, column=0, end_column=32, occurrence=1)
def test_multiline_async_dict_comprehension(self): def test_multiline_async_dict_comprehension(self):
snippet = """\ snippet = textwrap.dedent("""\
async def f(): async def f():
{x: {x:
2*x 2*x
async for x async for x
in [1,2,3] if (x > 0 in [1,2,3] if (x > 0
and x < 100 and x < 100
and x != 50)} and x != 50)}
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
g = {} g = {}
eval(compiled_code, g) eval(compiled_code, g)
@ -1464,11 +1466,11 @@ async def f():
line=2, end_line=7, column=4, end_column=36, occurrence=1) line=2, end_line=7, column=4, end_column=36, occurrence=1)
def test_matchcase_sequence(self): def test_matchcase_sequence(self):
snippet = """\ snippet = textwrap.dedent("""\
match x: match x:
case a, b: case a, b:
pass pass
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE', self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
line=2, end_line=2, column=9, end_column=13, occurrence=1) line=2, end_line=2, column=9, end_column=13, occurrence=1)
@ -1480,11 +1482,11 @@ match x:
line=2, end_line=2, column=9, end_column=13, occurrence=2) line=2, end_line=2, column=9, end_column=13, occurrence=2)
def test_matchcase_sequence_wildcard(self): def test_matchcase_sequence_wildcard(self):
snippet = """\ snippet = textwrap.dedent("""\
match x: match x:
case a, *b, c: case a, *b, c:
pass pass
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE', self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_SEQUENCE',
line=2, end_line=2, column=9, end_column=17, occurrence=1) line=2, end_line=2, column=9, end_column=17, occurrence=1)
@ -1498,11 +1500,11 @@ match x:
line=2, end_line=2, column=9, end_column=17, occurrence=3) line=2, end_line=2, column=9, end_column=17, occurrence=3)
def test_matchcase_mapping(self): def test_matchcase_mapping(self):
snippet = """\ snippet = textwrap.dedent("""\
match x: match x:
case {"a" : a, "b": b}: case {"a" : a, "b": b}:
pass pass
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING', self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
line=2, end_line=2, column=9, end_column=26, occurrence=1) line=2, end_line=2, column=9, end_column=26, occurrence=1)
@ -1514,11 +1516,11 @@ match x:
line=2, end_line=2, column=9, end_column=26, occurrence=2) line=2, end_line=2, column=9, end_column=26, occurrence=2)
def test_matchcase_mapping_wildcard(self): def test_matchcase_mapping_wildcard(self):
snippet = """\ snippet = textwrap.dedent("""\
match x: match x:
case {"a" : a, "b": b, **c}: case {"a" : a, "b": b, **c}:
pass pass
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING', self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_MAPPING',
line=2, end_line=2, column=9, end_column=31, occurrence=1) line=2, end_line=2, column=9, end_column=31, occurrence=1)
@ -1530,11 +1532,11 @@ match x:
line=2, end_line=2, column=9, end_column=31, occurrence=2) line=2, end_line=2, column=9, end_column=31, occurrence=2)
def test_matchcase_class(self): def test_matchcase_class(self):
snippet = """\ snippet = textwrap.dedent("""\
match x: match x:
case C(a, b): case C(a, b):
pass pass
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS', self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
line=2, end_line=2, column=9, end_column=16, occurrence=1) line=2, end_line=2, column=9, end_column=16, occurrence=1)
@ -1546,11 +1548,11 @@ match x:
line=2, end_line=2, column=9, end_column=16, occurrence=2) line=2, end_line=2, column=9, end_column=16, occurrence=2)
def test_matchcase_or(self): def test_matchcase_or(self):
snippet = """\ snippet = textwrap.dedent("""\
match x: match x:
case C(1) | C(2): case C(1) | C(2):
pass pass
""" """)
compiled_code, _ = self.check_positions_against_ast(snippet) compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS', self.assertOpcodeSourcePositionIs(compiled_code, 'MATCH_CLASS',
line=2, end_line=2, column=9, end_column=13, occurrence=1) line=2, end_line=2, column=9, end_column=13, occurrence=1)