Skip to content

Commit

Permalink
simplify scope handling
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Mar 7, 2023
1 parent 4109baa commit d8802a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
19 changes: 7 additions & 12 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5162,8 +5162,8 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
assert(c->u->u_fastlocals == NULL);
if (c->u->u_ste->ste_type != FunctionBlock) {
// comprehension in non-function scope; for isolation, we'll need to
// override names bound in the comprehension to use fast locals, even
// though nothing else in this frame will
// temporarily override names bound in the comprehension to use fast
// locals, even though nothing else in this frame will
c->u->u_fastlocals = PySet_New(0);
if (c->u->u_fastlocals == NULL) {
return ERROR;
Expand All @@ -5190,15 +5190,10 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
}
assert(PyLong_Check(outv));
long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK;
int outer_global = (outsc == GLOBAL_IMPLICIT || outsc == GLOBAL_EXPLICIT);
if (outer_global || ((outsc == CELL || outsc == FREE) && scope == LOCAL)) {
// If a name is global in the outer scope but local in the
// comprehension scope, we need to keep it global in outer scope
// but ensure the comprehension writes to the local, not the
// global; this is all the isolation we need. If it's a cell in
// outer scope and a local inside the comprehension, we want the
// comprehension to treat it as a local, but we also need to
// save/restore the outer cell.
if (scope != outsc) {
// If a name has different scope inside than outside the
// comprehension, we need to temporarily handle it with the
// right scope while compiling the comprehension.
if (state->temp_symbols == NULL) {
state->temp_symbols = PyDict_New();
if (state->temp_symbols == NULL) {
Expand All @@ -5219,7 +5214,7 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
}
Py_DECREF(outv);
}
if (!outer_global && (scope == LOCAL || scope == CELL)) {
if (outsc == LOCAL || outsc == CELL || outsc == FREE) {
// local names bound in comprehension must be isolated from
// outer scope; push existing value (which may be NULL if
// not defined) on stack
Expand Down
25 changes: 7 additions & 18 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,23 +605,12 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
}
SET_SCOPE(scopes, k, scope);
} else {
// name already exists in scope
PyObject *v_existing_scope = PyDict_GetItemWithError(scopes, k);
if (v_existing_scope == NULL) {
return 0;
}
long existing_scope = PyLong_AsLong(v_existing_scope);
// if name in comprehension was a cell, promote to cell
if (scope == CELL && existing_scope != CELL) {
SET_SCOPE(scopes, k, CELL);
} else {
// free vars in comprehension that are locals in outer scope can
// now simply be locals, unless they are free in comp children
if ((PyLong_AsLong(existing) & DEF_BOUND) &&
!is_free_in_children(comp, k)) {
if (PySet_Discard(comp_free, k) < 0) {
return 0;
}
// free vars in comprehension that are locals in outer scope can
// now simply be locals, unless they are free in comp children
if ((PyLong_AsLong(existing) & DEF_BOUND) &&
!is_free_in_children(comp, k)) {
if (PySet_Discard(comp_free, k) < 0) {
return 0;
}
}
}
Expand Down Expand Up @@ -906,7 +895,7 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
assert(c && PySTEntry_Check(c));
entry = (PySTEntryObject*)c;

// we inline comprehensions if inside a function and not a generator
// we inline all non-generator-expression comprehensions
int inline_comp =
entry->ste_comprehension &&
!entry->ste_generator;
Expand Down

0 comments on commit d8802a1

Please sign in to comment.