Skip to content

Commit d80384a

Browse files
committed
Tests.
1 parent 77251a2 commit d80384a

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

Lib/test/test_compile.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ def test_multiline_async_generator_expression(self):
20632063

20642064
def test_multiline_list_comprehension(self):
20652065
snippet = textwrap.dedent("""\
2066-
[(x,
2066+
_ = [(x,
20672067
2*x)
20682068
for x
20692069
in [1,2,3] if (x > 0
@@ -2073,14 +2073,14 @@ def test_multiline_list_comprehension(self):
20732073
compiled_code, _ = self.check_positions_against_ast(snippet)
20742074
self.assertIsInstance(compiled_code, types.CodeType)
20752075
self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
2076-
line=1, end_line=2, column=1, end_column=8, occurrence=1)
2076+
line=1, end_line=2, column=5, end_column=8, occurrence=1)
20772077
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2078-
line=1, end_line=2, column=1, end_column=8, occurrence=1)
2078+
line=1, end_line=2, column=5, end_column=8, occurrence=1)
20792079

20802080
def test_multiline_async_list_comprehension(self):
20812081
snippet = textwrap.dedent("""\
20822082
async def f():
2083-
[(x,
2083+
_ = [(x,
20842084
2*x)
20852085
async for x
20862086
in [1,2,3] if (x > 0
@@ -2093,15 +2093,15 @@ async def f():
20932093
compiled_code = g['f'].__code__
20942094
self.assertIsInstance(compiled_code, types.CodeType)
20952095
self.assertOpcodeSourcePositionIs(compiled_code, 'LIST_APPEND',
2096-
line=2, end_line=3, column=5, end_column=12, occurrence=1)
2096+
line=2, end_line=3, column=9, end_column=12, occurrence=1)
20972097
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2098-
line=2, end_line=3, column=5, end_column=12, occurrence=1)
2098+
line=2, end_line=3, column=9, end_column=12, occurrence=1)
20992099
self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2100-
line=2, end_line=7, column=4, end_column=36, occurrence=1)
2100+
line=2, end_line=2, column=4, end_column=5, occurrence=1)
21012101

21022102
def test_multiline_set_comprehension(self):
21032103
snippet = textwrap.dedent("""\
2104-
{(x,
2104+
_ = {(x,
21052105
2*x)
21062106
for x
21072107
in [1,2,3] if (x > 0
@@ -2111,9 +2111,9 @@ def test_multiline_set_comprehension(self):
21112111
compiled_code, _ = self.check_positions_against_ast(snippet)
21122112
self.assertIsInstance(compiled_code, types.CodeType)
21132113
self.assertOpcodeSourcePositionIs(compiled_code, 'SET_ADD',
2114-
line=1, end_line=2, column=1, end_column=8, occurrence=1)
2114+
line=1, end_line=2, column=5, end_column=8, occurrence=1)
21152115
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2116-
line=1, end_line=2, column=1, end_column=8, occurrence=1)
2116+
line=1, end_line=2, column=5, end_column=8, occurrence=1)
21172117

21182118
def test_multiline_async_set_comprehension(self):
21192119
snippet = textwrap.dedent("""\
@@ -2139,7 +2139,7 @@ async def f():
21392139

21402140
def test_multiline_dict_comprehension(self):
21412141
snippet = textwrap.dedent("""\
2142-
{x:
2142+
_ = {x:
21432143
2*x
21442144
for x
21452145
in [1,2,3] if (x > 0
@@ -2149,14 +2149,14 @@ def test_multiline_dict_comprehension(self):
21492149
compiled_code, _ = self.check_positions_against_ast(snippet)
21502150
self.assertIsInstance(compiled_code, types.CodeType)
21512151
self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
2152-
line=1, end_line=2, column=1, end_column=7, occurrence=1)
2152+
line=1, end_line=2, column=5, end_column=7, occurrence=1)
21532153
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2154-
line=1, end_line=2, column=1, end_column=7, occurrence=1)
2154+
line=1, end_line=2, column=5, end_column=7, occurrence=1)
21552155

21562156
def test_multiline_async_dict_comprehension(self):
21572157
snippet = textwrap.dedent("""\
21582158
async def f():
2159-
{x:
2159+
_ = {x:
21602160
2*x
21612161
async for x
21622162
in [1,2,3] if (x > 0
@@ -2169,11 +2169,11 @@ async def f():
21692169
compiled_code = g['f'].__code__
21702170
self.assertIsInstance(compiled_code, types.CodeType)
21712171
self.assertOpcodeSourcePositionIs(compiled_code, 'MAP_ADD',
2172-
line=2, end_line=3, column=5, end_column=11, occurrence=1)
2172+
line=2, end_line=3, column=9, end_column=11, occurrence=1)
21732173
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2174-
line=2, end_line=3, column=5, end_column=11, occurrence=1)
2174+
line=2, end_line=3, column=9, end_column=11, occurrence=1)
21752175
self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2176-
line=2, end_line=7, column=4, end_column=36, occurrence=1)
2176+
line=2, end_line=2, column=4, end_column=5, occurrence=1)
21772177

21782178
def test_matchcase_sequence(self):
21792179
snippet = textwrap.dedent("""\

Lib/test/test_compiler_codegen.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,39 @@ def test_frozenset_optimization(self):
192192
('RETURN_VALUE', None)
193193
]
194194
self.codegen_test(snippet, expected)
195+
196+
def test_no_target_comp_optimization(self):
197+
snippet = "[i for i in range(10)]"
198+
expected = [
199+
('RESUME', 0),
200+
('ANNOTATIONS_PLACEHOLDER', None),
201+
('LOAD_NAME', 0),
202+
('PUSH_NULL', None),
203+
('LOAD_CONST', 0),
204+
('CALL', 1),
205+
('LOAD_FAST_AND_CLEAR', 0),
206+
('SWAP', 2),
207+
('SETUP_FINALLY', 20),
208+
('COPY', 1),
209+
('GET_ITER', 0),
210+
('FOR_ITER', 16),
211+
('STORE_FAST', 0),
212+
('LOAD_FAST', 0),
213+
('POP_TOP', None),
214+
('JUMP', 11),
215+
('END_FOR', None),
216+
('POP_ITER', None),
217+
('POP_BLOCK', None),
218+
('JUMP_NO_INTERRUPT', 25),
219+
('SWAP', 2),
220+
('POP_TOP', None),
221+
('SWAP', 2),
222+
('STORE_FAST_MAYBE_NULL', 0),
223+
('RERAISE', 0),
224+
('SWAP', 2),
225+
('STORE_FAST_MAYBE_NULL', 0),
226+
('POP_TOP', None),
227+
('LOAD_CONST', 1),
228+
('RETURN_VALUE', None),
229+
]
230+
self.codegen_test(snippet, expected)

0 commit comments

Comments
 (0)