Skip to content

Commit cf1bb55

Browse files
committed
Remove dict and set stuff.
This was a breaking change because hash() was no longer called on the elements.
1 parent d2749ae commit cf1bb55

3 files changed

Lines changed: 16 additions & 49 deletions

File tree

Lib/test/test_compile.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ async def f():
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=5, end_column=8, occurrence=1)
2114+
line=1, end_line=2, column=1, end_column=8, occurrence=1)
21152115
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2116-
line=1, end_line=2, column=5, end_column=8, occurrence=1)
2116+
line=1, end_line=2, column=1, 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=5, end_column=7, occurrence=1)
2152+
line=1, end_line=2, column=1, end_column=7, occurrence=1)
21532153
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2154-
line=1, end_line=2, column=5, end_column=7, occurrence=1)
2154+
line=1, end_line=2, column=1, 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=9, end_column=11, occurrence=1)
2172+
line=2, end_line=3, column=5, end_column=11, occurrence=1)
21732173
self.assertOpcodeSourcePositionIs(compiled_code, 'JUMP_BACKWARD',
2174-
line=2, end_line=3, column=9, end_column=11, occurrence=1)
2174+
line=2, end_line=3, column=5, end_column=11, occurrence=1)
21752175
self.assertOpcodeSourcePositionIs(compiled_code, 'RETURN_VALUE',
2176-
line=2, end_line=2, column=4, end_column=5, occurrence=1)
2176+
line=2, end_line=7, column=4, end_column=36, occurrence=1)
21772177

21782178
def test_matchcase_sequence(self):
21792179
snippet = textwrap.dedent("""\
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Avoid creating :class:`list`, :class:`set`, and :class:`dict` objects in
2-
comprehensions when the comprehension is not used as a value.
1+
Avoid creating :class:`list` objects in comprehensions when the comprehension
2+
is not used as a value.

Python/codegen.c

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ typedef enum {
235235

236236
static int
237237
codegen_listcomp_impl(compiler *c, expr_ty e, bool avoid_creation);
238-
static int
239-
codegen_setcomp_impl(compiler *c, expr_ty e, bool avoid_creation);
240-
static int
241-
codegen_dictcomp_impl(compiler *c, expr_ty e, bool avoid_creation);
242238
static int codegen_sync_comprehension_generator(
243239
compiler *c, location loc,
244240
asdl_comprehension_seq *generators, int gen_index,
@@ -3096,14 +3092,6 @@ codegen_stmt_expr(compiler *c, location loc, expr_ty value)
30963092
return codegen_listcomp_impl(c, value, /*avoid_creation=*/true);
30973093
}
30983094

3099-
if (value->kind == SetComp_kind) {
3100-
return codegen_setcomp_impl(c, value, /*avoid_creation=*/true);
3101-
}
3102-
3103-
if (value->kind == DictComp_kind) {
3104-
return codegen_dictcomp_impl(c, value, /*avoid_creation=*/true);
3105-
}
3106-
31073095
VISIT(c, expr, value);
31083096
ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */
31093097
return SUCCESS;
@@ -4696,11 +4684,6 @@ codegen_sync_comprehension_generator(compiler *c, location loc,
46964684
}
46974685
break;
46984686
case COMP_SETCOMP:
4699-
if (avoid_creation) {
4700-
VISIT(c, expr, elt);
4701-
ADDOP(c, elt_loc, POP_TOP);
4702-
break;
4703-
}
47044687
if (elt->kind == Starred_kind) {
47054688
VISIT(c, expr, elt->v.Starred.value);
47064689
ADDOP_I(c, elt_loc, SET_UPDATE, depth + 1);
@@ -4711,11 +4694,6 @@ codegen_sync_comprehension_generator(compiler *c, location loc,
47114694
}
47124695
break;
47134696
case COMP_DICTCOMP:
4714-
if (avoid_creation) {
4715-
VISIT(c, expr, elt);
4716-
ADDOP(c, elt_loc, POP_TOP);
4717-
break;
4718-
}
47194697
if (val == NULL) {
47204698
/* unpacking (**) case */
47214699
VISIT(c, expr, elt);
@@ -5210,35 +5188,24 @@ codegen_listcomp(compiler *c, expr_ty e)
52105188
}
52115189

52125190
static int
5213-
codegen_setcomp_impl(compiler *c, expr_ty e, bool avoid_creation)
5191+
codegen_setcomp(compiler *c, expr_ty e)
52145192
{
52155193
assert(e->kind == SetComp_kind);
52165194
_Py_DECLARE_STR(anon_setcomp, "<setcomp>");
52175195
return codegen_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
52185196
e->v.SetComp.generators,
5219-
e->v.SetComp.elt, NULL, avoid_creation);
5197+
e->v.SetComp.elt, NULL, /*avoid_creation=*/false);
52205198
}
52215199

5222-
static int
5223-
codegen_setcomp(compiler *c, expr_ty e)
5224-
{
5225-
return codegen_setcomp_impl(c, e, false);
5226-
}
52275200

52285201
static int
5229-
codegen_dictcomp_impl(compiler *c, expr_ty e, bool avoid_creation)
5202+
codegen_dictcomp(compiler *c, expr_ty e)
52305203
{
52315204
assert(e->kind == DictComp_kind);
52325205
_Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
52335206
return codegen_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
52345207
e->v.DictComp.generators,
5235-
e->v.DictComp.key, e->v.DictComp.value, avoid_creation);
5236-
}
5237-
5238-
static int
5239-
codegen_dictcomp(compiler *c, expr_ty e)
5240-
{
5241-
return codegen_dictcomp_impl(c, e, false);
5208+
e->v.DictComp.key, e->v.DictComp.value, /*avoid_creation=*/false);
52425209
}
52435210

52445211

0 commit comments

Comments
 (0)