Skip to content

Commit 6fd3527

Browse files
authored
Fix binding freevars in comprehension scope (#1073)
* Fix binding freevars in comprehension scope [lambda: i for i in [1,2]] should bind the `i` correctly now. * Enable test_listcomp and test_setcomp * update DLR
1 parent 2b4f326 commit 6fd3527

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

Src/IronPython/Compiler/Ast/Comprehension.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,28 @@ internal override bool ExposesLocalVariable(PythonVariable variable) {
237237
return MSAst.Expression.Call(null, typeof(PythonOps).GetMethod(nameof(PythonOps.GetClosureTupleFromContext)), _comprehension.Parent.LocalContext);
238238
}
239239

240+
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) {
241+
ContainsNestedFreeVariables = true;
242+
if (TryGetVariable(reference.Name, out variable)) {
243+
Debug.Assert(variable.Kind != VariableKind.Nonlocal, "there should be no nonlocals in a comprehension");
244+
variable.AccessedInNestedScope = true;
245+
246+
if (variable.Kind == VariableKind.Local || variable.Kind == VariableKind.Parameter) {
247+
from.AddFreeVariable(variable, true);
248+
249+
for (ScopeStatement scope = from.Parent; scope != this; scope = scope.Parent) {
250+
scope.AddFreeVariable(variable, false);
251+
}
252+
253+
AddCellVariable(variable);
254+
} else {
255+
from.AddReferencedGlobal(reference.Name);
256+
}
257+
return true;
258+
}
259+
return false;
260+
}
261+
240262
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) {
241263
if (TryGetVariable(reference.Name, out PythonVariable variable)) {
242264
if (variable.Kind == VariableKind.Global) {

Src/IronPythonTest/Cases/CPythonCasesManifest.ini

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,6 @@ Ignore=true
570570
IsolationLevel=ENGINE
571571
MaxRecursion=100
572572

573-
[CPython.test_listcomps]
574-
Ignore=true
575-
576573
[CPython.test_locale]
577574
Ignore=true
578575

@@ -831,9 +828,6 @@ Ignore=true
831828
RunCondition=NOT $(IS_MONO) # weakref failures; https://github.com/IronLanguages/ironpython3/issues/544
832829
IsolationLevel=PROCESS # Also weakref failures; https://github.com/IronLanguages/ironpython3/issues/489
833830

834-
[CPython.test_setcomps]
835-
Ignore=true
836-
837831
[CPython.test_shelve]
838832
NotParallelSafe=true
839833

0 commit comments

Comments
 (0)