Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-104374: Remove access to class scopes for inlined comprehensions#104528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
3fad7b0
gh-104374: Remove access to class scopes for inlined comprehensions
JelleZijlstrafa11ca9
Update Lib/test/test_listcomps.py
JelleZijlstra1d776fd
Remove unnecessary change
JelleZijlstra932a2da
Better tests, copied from Carl's PR
JelleZijlstrad5653ae
A few more tests
JelleZijlstraba3bacf
Merge remote-tracking branch 'upstream/main' into classcomp2
JelleZijlstra948bc1a
fix definition of 'in a class block'
carljm045478f
a couple more tests
carljmfc63272
Merge remote-tracking branch 'upstream/main' into classcomp2
JelleZijlstraafe0277
A few more tests
JelleZijlstraFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
21 changes: 19 additions & 2 deletionsLib/test/test_listcomps.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -200,7 +200,7 @@ def f(): | ||
y = [g for x in [1]] | ||
""" | ||
outputs = {"y": [2]} | ||
self._check_in_scopes(code, outputs, scopes=["module", "function"]) | ||
def test_inner_cell_shadows_outer_redefined(self): | ||
code = """ | ||
@@ -328,7 +328,7 @@ def test_nested_2(self): | ||
y = [x for [x ** x for x in range(x)][x - 1] in l] | ||
""" | ||
outputs = {"y": [3, 3, 3]} | ||
self._check_in_scopes(code, outputs, scopes=["module", "function"]) | ||
def test_nested_3(self): | ||
code = """ | ||
@@ -379,6 +379,23 @@ def f(): | ||
with self.assertRaises(UnboundLocalError): | ||
f() | ||
def test_unbound_local_in_class_scope(self): | ||
class X: | ||
y = 1 | ||
with self.assertRaises(NameError): | ||
[x + y for x in range(2)] | ||
def test_comprehension_in_class_scope(self): | ||
code = """ | ||
y = 1 | ||
class X: | ||
y = 2 | ||
vals = [(x, y) for x in range(2)] | ||
vals = X.vals | ||
""" | ||
self._check_in_scopes(code, {"vals": [(0, 1), (1, 1)]}, | ||
scopes=["module", "fucntion"]) | ||
JelleZijlstra marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
carljm marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
__test__ = {'doctests' : doctests} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -388,6 +388,8 @@ struct compiler_unit { | ||
instr_sequence u_instr_sequence; /* codegen output */ | ||
int u_nfblocks; | ||
int u_in_inlined_comp; | ||
struct fblockinfo u_fblock[CO_MAXBLOCKS]; | ||
_PyCompile_CodeUnitMetadata u_metadata; | ||
@@ -1290,6 +1292,7 @@ compiler_enter_scope(struct compiler *c, identifier name, | ||
} | ||
u->u_nfblocks = 0; | ||
u->u_in_inlined_comp = 0; | ||
u->u_metadata.u_firstlineno = lineno; | ||
u->u_metadata.u_consts = PyDict_New(); | ||
if (!u->u_metadata.u_consts) { | ||
@@ -4137,7 +4140,7 @@ compiler_nameop(struct compiler *c, location loc, | ||
case OP_DEREF: | ||
switch (ctx) { | ||
case Load: | ||
if (c->u->u_ste->ste_type == ClassBlock && !c->u->u_in_inlined_comp) { | ||
op = LOAD_FROM_DICT_OR_DEREF; | ||
// First load the locals | ||
if (codegen_addop_noarg(INSTR_SEQUENCE(c), LOAD_LOCALS, loc) < 0) { | ||
@@ -4188,7 +4191,12 @@ compiler_nameop(struct compiler *c, location loc, | ||
break; | ||
case OP_NAME: | ||
switch (ctx) { | ||
case Load: | ||
op = (c->u->u_ste->ste_type == ClassBlock | ||
&& c->u->u_in_inlined_comp) | ||
? LOAD_GLOBAL | ||
: LOAD_NAME; | ||
break; | ||
case Store: op = STORE_NAME; break; | ||
case Del: op = DELETE_NAME; break; | ||
} | ||
@@ -5413,6 +5421,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc, | ||
PySTEntryObject *entry, | ||
inlined_comprehension_state *state) | ||
{ | ||
c->u->u_in_inlined_comp++; | ||
// iterate over names bound in the comprehension and ensure we isolate | ||
// them from the outer scope as needed | ||
PyObject *k, *v; | ||
@@ -5424,7 +5433,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc, | ||
// at all; DEF_LOCAL | DEF_NONLOCAL can occur in the case of an | ||
// assignment expression to a nonlocal in the comprehension, these don't | ||
// need handling here since they shouldn't be isolated | ||
if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) || c->u->u_ste->ste_type == ClassBlock) { | ||
if (!_PyST_IsFunctionLike(c->u->u_ste)) { | ||
// non-function scope: override this name to use fast locals | ||
PyObject *orig = PyDict_GetItem(c->u->u_metadata.u_fasthidden, k); | ||
@@ -5444,10 +5453,12 @@ push_inlined_comprehension_state(struct compiler *c, location loc, | ||
} | ||
} | ||
long scope = (symbol >> SCOPE_OFFSET) & SCOPE_MASK; | ||
if (scope == FREE && c->u->u_ste->ste_type == ClassBlock) { | ||
dict_add_o(c->u->u_metadata.u_freevars, k); | ||
JelleZijlstra marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
PyObject *outv = PyDict_GetItemWithError(c->u->u_ste->ste_symbols, k); | ||
if (outv == NULL) { | ||
outv = _PyLong_GetZero(); | ||
} | ||
assert(PyLong_Check(outv)); | ||
long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK; | ||
@@ -5521,6 +5532,7 @@ static int | ||
pop_inlined_comprehension_state(struct compiler *c, location loc, | ||
inlined_comprehension_state state) | ||
{ | ||
c->u->u_in_inlined_comp--; | ||
PyObject *k, *v; | ||
Py_ssize_t pos = 0; | ||
if (state.temp_symbols) { | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.