summaryrefslogtreecommitdiffstats
path: root/Python/symtable.c
diff options
context:
space:
mode:
authorCarl Meyer <carl@oddbird.net>2023-05-11 01:08:40 (GMT)
committerGitHub <noreply@github.com>2023-05-11 01:08:40 (GMT)
commitfcd5fb49b1d71165f3c503c3d2e74a082ddb2f21 (patch)
treeb806e49c3b5bb80f82c9b5a32d5c3ee916efc6d2 /Python/symtable.c
parent94f30c75576bb8a20724b2ac758fa33af089a522 (diff)
downloadcpython-fcd5fb49b1d71165f3c503c3d2e74a082ddb2f21.zip
cpython-fcd5fb49b1d71165f3c503c3d2e74a082ddb2f21.tar.gz
cpython-fcd5fb49b1d71165f3c503c3d2e74a082ddb2f21.tar.bz2
gh-104357: fix inlined comprehensions that close over iteration var (#104368)
Diffstat (limited to 'Python/symtable.c')
-rw-r--r--Python/symtable.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index 6e74d76..9361674 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -607,12 +607,19 @@ inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
SET_SCOPE(scopes, k, scope);
}
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_any_child(comp, k)) {
- if (PySet_Discard(comp_free, k) < 0) {
- return 0;
+ if (PyLong_AsLong(existing) & DEF_BOUND) {
+ // cell vars in comprehension that are locals in outer scope
+ // must be promoted to cell so u_cellvars isn't wrong
+ if (scope == CELL && ste->ste_type == FunctionBlock) {
+ SET_SCOPE(scopes, k, scope);
+ }
+
+ // free vars in comprehension that are locals in outer scope can
+ // now simply be locals, unless they are free in comp children
+ if (!is_free_in_any_child(comp, k)) {
+ if (PySet_Discard(comp_free, k) < 0) {
+ return 0;
+ }
}
}
}