Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6a025b

Browse files
committed
add tests for comprehensions in class scope
1 parent 24a9d9f commit b6a025b

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

Lib/test/test_listcomps.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
>>> [x() for x in items]
116116
[2, 2, 2, 2, 2]
117117
118-
We also repeat each of the above scoping tests inside a function
118+
We also repeat each of the above scoping tests inside a function:
119119
120120
>>> def test_func():
121121
... items = [(lambda i=i: i) for i in range(5)]
@@ -143,7 +143,37 @@
143143
>>> test_func()
144144
[2, 2, 2, 2, 2]
145145
146-
Some more tests for scoping edge cases:
146+
And in class scope:
147+
148+
>>> class C:
149+
... items = [(lambda i=i: i) for i in range(5)]
150+
... ret = [x() for x in items]
151+
>>> C.ret
152+
[0, 1, 2, 3, 4]
153+
154+
>>> class C:
155+
... items = [(lambda: i) for i in range(5)]
156+
... ret = [x() for x in items]
157+
>>> C.ret
158+
[4, 4, 4, 4, 4]
159+
160+
>>> class C:
161+
... items = [(lambda: i) for i in range(5)]
162+
... i = 20
163+
... ret = [x() for x in items]
164+
>>> C.ret
165+
[4, 4, 4, 4, 4]
166+
>>> C.i
167+
20
168+
169+
>>> class C:
170+
... items = [(lambda: y) for i in range(5)]
171+
... y = 2
172+
... ret = [x() for x in items]
173+
>>> C.ret
174+
[2, 2, 2, 2, 2]
175+
176+
Some more tests for scoping edge cases, each in func/module/class scope:
147177
148178
>>> def test_func():
149179
... y = 10

Python/compile.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,7 +2670,6 @@ compiler_class(struct compiler *c, stmt_ty s)
26702670
}
26712671
else {
26722672
/* No methods referenced __class__, so just return None */
2673-
assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
26742673
ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
26752674
}
26762675
ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE);
@@ -5318,10 +5317,8 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
53185317
// need handling here since they shouldn't be isolated
53195318
if (symbol & DEF_LOCAL && ~symbol & DEF_NONLOCAL) {
53205319
if (c->u->u_fastlocals) {
5321-
// non-function scope: override this name to use fast locals,
5322-
// and that's all we need
5320+
// non-function scope: override this name to use fast locals
53235321
PySet_Add(c->u->u_fastlocals, k);
5324-
continue;
53255322
}
53265323
long scope = (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
53275324
PyObject *outv = PyDict_GetItemWithError(c->u->u_ste->ste_symbols, k);

0 commit comments

Comments
 (0)