Skip to content

Commit 3749b58

Browse files
committed
[GR-31682] Github #101: Issue with list comprehension
PullRequest: graalpython/1814
2 parents e945f16 + f1cf36e commit 3749b58

File tree

13 files changed

+667
-2
lines changed

13 files changed

+667
-2
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/GeneratorAndCompForTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,29 @@ public void dictCompr() throws Exception {
349349
public void genExpr() throws Exception {
350350
checkScopeAndTree("(i for i in range(3))");
351351
}
352+
353+
@Test
354+
public void genExpr01() throws Exception {
355+
checkScopeAndTree("[ (c,s) for c in ('a','b') for s in (1,2,3,4,5)]");
356+
}
357+
358+
@Test
359+
public void genExpr02() throws Exception {
360+
checkScopeAndTree("[ (s,c) for c in ('a','b') for s in (1,2,3,4,5)]");
361+
}
362+
363+
@Test
364+
public void genExpr03() throws Exception {
365+
checkScopeAndTree("(e+1 for e in (i*2 for i in (1,2,3)))");
366+
}
367+
368+
@Test
369+
public void issueGitHub_42_v1() throws Exception {
370+
checkScopeAndTree("[e for e in (s for s in (1, 2, 3))]");
371+
}
372+
373+
@Test
374+
public void issueGitHub_42_v2() throws Exception {
375+
checkScopeAndTree("[e for e in [s for s in (1, 2, 3)]]");
376+
}
352377
}

graalpython/com.oracle.graal.python.test/src/tests/test_parser.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,26 @@ def fn4() : a = 1; b = (1,2,3); c = 4; return (1, 2, 3, 1, 2, 3)
856856
assert 2 not in fn4.__code__.co_consts
857857
assert find_count_in(fn4.__code__.co_consts, 1) == 1
858858
assert find_count_in(fn4.__code__.co_consts, 4) == 1
859+
860+
def test_ComprehensionGeneratorExpr():
861+
def create_list(gen):
862+
result = []
863+
for i in gen:
864+
result.append(i)
865+
return result
866+
867+
gen = (i for i in range(3))
868+
assert [0,1,2] == create_list(gen)
869+
gen = (e+1 for e in (i*2 for i in (1,2,3)))
870+
assert [3,5,7] == create_list(gen)
871+
gen = ((c,s) for c in ('a','b') for s in (1,2))
872+
assert [('a', 1), ('a', 2), ('b', 1), ('b', 2)] == create_list(gen)
873+
gen = ((s,c) for c in ('a','b') for s in (1,2))
874+
assert [(1, 'a'), (2, 'a'), (1, 'b'), (2, 'b')] == create_list(gen)
875+
876+
def test_ComprehensionListExpr():
877+
assert [3,5,7] == [e+1 for e in (i*2 for i in (1,2,3))]
878+
assert [3,5,7] == [e+1 for e in [i*2 for i in (1,2,3)]]
879+
assert [('a', 1), ('a', 2), ('b', 1), ('b', 2)] == [ (c,s) for c in ('a','b') for s in (1,2)]
880+
assert [(1, 'a'), (2, 'a'), (1, 'b'), (2, 'b')] == [ (s,c) for c in ('a','b') for s in (1,2)]
881+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Scope: []
2+
Kind: Module
3+
FrameDescriptor: Empty
4+
CellVars: Empty
5+
FreeVars: Empty
6+
Scope: <genexpr>
7+
Kind: ListComp
8+
FrameDescriptor: [s, c, <return_val>]
9+
CellVars: Empty
10+
FreeVars: Empty
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
ModuleRootNode Name: <module 'genExpr01'> SourceSection: [0,48]`[ (c,s) for c in ('a...`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,48]`[ (c,s) for c in ('a...`
8+
PythonCallUnary SourceSection: [0,48]`[ (c,s) for c in ('a...`
9+
CallUnaryMethodNodeGen SourceSection: None
10+
ReadGlobalOrBuiltinNodeGen SourceSection: None
11+
Identifier: list
12+
ReadAttributeFromObjectNotTypeNodeGen SourceSection: None
13+
GeneratorExpressionNode SourceSection: [2,47]`(c,s) for c in ('a',...`
14+
Name: <genexpr>
15+
FrameDescriptor: 3 slots [s, c, <return_val>]
16+
Enclosing
17+
FrameDescriptor: Empty
18+
Active Flags: 2
19+
For Nodes: 2
20+
Block Nodes: 0
21+
Is Enclosing Frame Generator: false
22+
FunctionRootNode SourceSection: [2,47]`(c,s) for c in ('a',...`
23+
Name: <genexpr>
24+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
25+
CelVars: None
26+
FreeVars: None
27+
NeedsCellFrame: False
28+
FrameDescriptor: 3 slots [s, c, <return_val>]
29+
ExecutionSlots:
30+
FreeVarsSlots: None
31+
CellVarsSlots: None
32+
InnerRootNode SourceSection: [2,47]`(c,s) for c in ('a',...`
33+
GeneratorReturnTargetNode SourceSection: [2,47]`(c,s) for c in ('a',...`
34+
flagSlot: 1
35+
GeneratorForNode SourceSection: [2,47]`(c,s) for c in ('a',...`
36+
GeneratorForNode SourceSection: [17,47]`('a','b') for s in (...`
37+
ExpressionStatementNode SourceSection: [2,7]`(c,s)`
38+
YieldNode SourceSection: [2,7]`(c,s)`
39+
flagSlot: 0
40+
TupleLiteralNode SourceSection: [2,7]`(c,s)`
41+
ReadGeneratorFrameVariableNodeGen SourceSection: [3,4]`c`
42+
Frame: [1,c,Illegal]
43+
ReadGeneratorFrameVariableNodeGen SourceSection: [5,6]`s`
44+
Frame: [0,s,Illegal]
45+
PythonObjectFactoryNodeGen SourceSection: None
46+
GeneratorAccessNode SourceSection: None
47+
WriteGeneratorFrameVariableNodeGen SourceSection: None
48+
Identifier: s
49+
Frame: [0,s,Illegal]
50+
GetIteratorExpressionNodeGen SourceSection: [36,47]`(1,2,3,4,5)`
51+
TupleLiteralNode SourceSection: [36,47]`(1,2,3,4,5)`
52+
IntegerLiteralNode SourceSection: [37,38]`1`
53+
Value: 1
54+
IntegerLiteralNode SourceSection: [39,40]`2`
55+
Value: 2
56+
IntegerLiteralNode SourceSection: [41,42]`3`
57+
Value: 3
58+
IntegerLiteralNode SourceSection: [43,44]`4`
59+
Value: 4
60+
IntegerLiteralNode SourceSection: [45,46]`5`
61+
Value: 5
62+
PythonObjectFactoryNodeGen SourceSection: None
63+
GetNextCached SourceSection: None
64+
LookupAndCallUnaryNodeGen SourceSection: None
65+
Op: __next__
66+
GeneratorAccessNode SourceSection: None
67+
IsBuiltinClassProfile SourceSection: None
68+
GetClassNodeGen SourceSection: None
69+
WriteGeneratorFrameVariableNodeGen SourceSection: None
70+
Identifier: c
71+
Frame: [1,c,Illegal]
72+
ArgumentExpressionNode SourceSection: None
73+
ReadIndexedArgumentNodeGen SourceSection: None
74+
Index: 0
75+
GetNextCached SourceSection: None
76+
LookupAndCallUnaryNodeGen SourceSection: None
77+
Op: __next__
78+
GeneratorAccessNode SourceSection: None
79+
IsBuiltinClassProfile SourceSection: None
80+
GetClassNodeGen SourceSection: None
81+
ReadGeneratorFrameVariableNodeGen SourceSection: None
82+
Frame: [2,<return_val>,Illegal]
83+
BlockNode SourceSection: None
84+
GeneratorAccessNode SourceSection: None
85+
PRaiseNodeGen SourceSection: None
86+
GetIteratorExpressionNodeGen SourceSection: [17,26]`('a','b')`
87+
TupleLiteralNode SourceSection: [17,26]`('a','b')`
88+
StringLiteralNode SourceSection: [18,21]`'a'`
89+
StringLiteralNode SourceSection: [22,25]`'b'`
90+
PythonObjectFactoryNodeGen SourceSection: None
91+
PythonObjectFactoryNodeGen SourceSection: None
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Scope: []
2+
Kind: Module
3+
FrameDescriptor: Empty
4+
CellVars: Empty
5+
FreeVars: Empty
6+
Scope: <genexpr>
7+
Kind: ListComp
8+
FrameDescriptor: [s, c, <return_val>]
9+
CellVars: Empty
10+
FreeVars: Empty
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
ModuleRootNode Name: <module 'genExpr02'> SourceSection: [0,48]`[ (s,c) for c in ('a...`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,48]`[ (s,c) for c in ('a...`
8+
PythonCallUnary SourceSection: [0,48]`[ (s,c) for c in ('a...`
9+
CallUnaryMethodNodeGen SourceSection: None
10+
ReadGlobalOrBuiltinNodeGen SourceSection: None
11+
Identifier: list
12+
ReadAttributeFromObjectNotTypeNodeGen SourceSection: None
13+
GeneratorExpressionNode SourceSection: [2,47]`(s,c) for c in ('a',...`
14+
Name: <genexpr>
15+
FrameDescriptor: 3 slots [s, c, <return_val>]
16+
Enclosing
17+
FrameDescriptor: Empty
18+
Active Flags: 2
19+
For Nodes: 2
20+
Block Nodes: 0
21+
Is Enclosing Frame Generator: false
22+
FunctionRootNode SourceSection: [2,47]`(s,c) for c in ('a',...`
23+
Name: <genexpr>
24+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
25+
CelVars: None
26+
FreeVars: None
27+
NeedsCellFrame: False
28+
FrameDescriptor: 3 slots [s, c, <return_val>]
29+
ExecutionSlots:
30+
FreeVarsSlots: None
31+
CellVarsSlots: None
32+
InnerRootNode SourceSection: [2,47]`(s,c) for c in ('a',...`
33+
GeneratorReturnTargetNode SourceSection: [2,47]`(s,c) for c in ('a',...`
34+
flagSlot: 1
35+
GeneratorForNode SourceSection: [2,47]`(s,c) for c in ('a',...`
36+
GeneratorForNode SourceSection: [17,47]`('a','b') for s in (...`
37+
ExpressionStatementNode SourceSection: [2,7]`(s,c)`
38+
YieldNode SourceSection: [2,7]`(s,c)`
39+
flagSlot: 0
40+
TupleLiteralNode SourceSection: [2,7]`(s,c)`
41+
ReadGeneratorFrameVariableNodeGen SourceSection: [3,4]`s`
42+
Frame: [0,s,Illegal]
43+
ReadGeneratorFrameVariableNodeGen SourceSection: [5,6]`c`
44+
Frame: [1,c,Illegal]
45+
PythonObjectFactoryNodeGen SourceSection: None
46+
GeneratorAccessNode SourceSection: None
47+
WriteGeneratorFrameVariableNodeGen SourceSection: None
48+
Identifier: s
49+
Frame: [0,s,Illegal]
50+
GetIteratorExpressionNodeGen SourceSection: [36,47]`(1,2,3,4,5)`
51+
TupleLiteralNode SourceSection: [36,47]`(1,2,3,4,5)`
52+
IntegerLiteralNode SourceSection: [37,38]`1`
53+
Value: 1
54+
IntegerLiteralNode SourceSection: [39,40]`2`
55+
Value: 2
56+
IntegerLiteralNode SourceSection: [41,42]`3`
57+
Value: 3
58+
IntegerLiteralNode SourceSection: [43,44]`4`
59+
Value: 4
60+
IntegerLiteralNode SourceSection: [45,46]`5`
61+
Value: 5
62+
PythonObjectFactoryNodeGen SourceSection: None
63+
GetNextCached SourceSection: None
64+
LookupAndCallUnaryNodeGen SourceSection: None
65+
Op: __next__
66+
GeneratorAccessNode SourceSection: None
67+
IsBuiltinClassProfile SourceSection: None
68+
GetClassNodeGen SourceSection: None
69+
WriteGeneratorFrameVariableNodeGen SourceSection: None
70+
Identifier: c
71+
Frame: [1,c,Illegal]
72+
ArgumentExpressionNode SourceSection: None
73+
ReadIndexedArgumentNodeGen SourceSection: None
74+
Index: 0
75+
GetNextCached SourceSection: None
76+
LookupAndCallUnaryNodeGen SourceSection: None
77+
Op: __next__
78+
GeneratorAccessNode SourceSection: None
79+
IsBuiltinClassProfile SourceSection: None
80+
GetClassNodeGen SourceSection: None
81+
ReadGeneratorFrameVariableNodeGen SourceSection: None
82+
Frame: [2,<return_val>,Illegal]
83+
BlockNode SourceSection: None
84+
GeneratorAccessNode SourceSection: None
85+
PRaiseNodeGen SourceSection: None
86+
GetIteratorExpressionNodeGen SourceSection: [17,26]`('a','b')`
87+
TupleLiteralNode SourceSection: [17,26]`('a','b')`
88+
StringLiteralNode SourceSection: [18,21]`'a'`
89+
StringLiteralNode SourceSection: [22,25]`'b'`
90+
PythonObjectFactoryNodeGen SourceSection: None
91+
PythonObjectFactoryNodeGen SourceSection: None
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Scope: []
2+
Kind: Module
3+
FrameDescriptor: Empty
4+
CellVars: Empty
5+
FreeVars: Empty
6+
Scope: <genexpr>
7+
Kind: GenExp
8+
FrameDescriptor: [i, <return_val>]
9+
CellVars: Empty
10+
FreeVars: Empty
11+
Scope: <genexpr>
12+
Kind: GenExp
13+
FrameDescriptor: [e, <return_val>]
14+
CellVars: Empty
15+
FreeVars: Empty
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
ModuleRootNode Name: <module 'genExpr03'> SourceSection: [0,37]`(e+1 for e in (i*2 f...`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,37]`(e+1 for e in (i*2 f...`
8+
GeneratorExpressionNode SourceSection: [0,37]`(e+1 for e in (i*2 f...`
9+
Name: <genexpr>
10+
FrameDescriptor: 2 slots [e, <return_val>]
11+
Enclosing
12+
FrameDescriptor: Empty
13+
Active Flags: 2
14+
For Nodes: 1
15+
Block Nodes: 0
16+
Is Enclosing Frame Generator: false
17+
FunctionRootNode SourceSection: [1,36]`e+1 for e in (i*2 fo...`
18+
Name: <genexpr>
19+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
20+
CelVars: None
21+
FreeVars: None
22+
NeedsCellFrame: False
23+
FrameDescriptor: 2 slots [e, <return_val>]
24+
ExecutionSlots:
25+
FreeVarsSlots: None
26+
CellVarsSlots: None
27+
InnerRootNode SourceSection: [1,36]`e+1 for e in (i*2 fo...`
28+
GeneratorReturnTargetNode SourceSection: [1,36]`e+1 for e in (i*2 fo...`
29+
flagSlot: 1
30+
GeneratorForNode SourceSection: [1,36]`e+1 for e in (i*2 fo...`
31+
ExpressionStatementNode SourceSection: [1,4]`e+1`
32+
YieldNode SourceSection: [1,4]`e+1`
33+
flagSlot: 0
34+
AddNodeGen SourceSection: [1,4]`e+1`
35+
ReadGeneratorFrameVariableNodeGen SourceSection: [1,2]`e`
36+
Frame: [0,e,Illegal]
37+
IntegerLiteralNode SourceSection: [3,4]`1`
38+
Value: 1
39+
GeneratorAccessNode SourceSection: None
40+
WriteGeneratorFrameVariableNodeGen SourceSection: None
41+
Identifier: e
42+
Frame: [0,e,Illegal]
43+
ArgumentExpressionNode SourceSection: None
44+
ReadIndexedArgumentNodeGen SourceSection: None
45+
Index: 0
46+
GetNextCached SourceSection: None
47+
LookupAndCallUnaryNodeGen SourceSection: None
48+
Op: __next__
49+
GeneratorAccessNode SourceSection: None
50+
IsBuiltinClassProfile SourceSection: None
51+
GetClassNodeGen SourceSection: None
52+
ReadGeneratorFrameVariableNodeGen SourceSection: None
53+
Frame: [1,<return_val>,Illegal]
54+
BlockNode SourceSection: None
55+
GeneratorAccessNode SourceSection: None
56+
PRaiseNodeGen SourceSection: None
57+
GetIteratorExpressionNodeGen SourceSection: [15,35]`i*2 for i in (1,2,3)...`
58+
GeneratorExpressionNode SourceSection: [15,35]`i*2 for i in (1,2,3)...`
59+
Name: <genexpr>
60+
FrameDescriptor: 2 slots [i, <return_val>]
61+
Enclosing
62+
FrameDescriptor: Empty
63+
Active Flags: 2
64+
For Nodes: 1
65+
Block Nodes: 0
66+
Is Enclosing Frame Generator: true
67+
FunctionRootNode SourceSection: [15,35]`i*2 for i in (1,2,3)...`
68+
Name: <genexpr>
69+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
70+
CelVars: None
71+
FreeVars: None
72+
NeedsCellFrame: False
73+
FrameDescriptor: 2 slots [i, <return_val>]
74+
ExecutionSlots:
75+
FreeVarsSlots: None
76+
CellVarsSlots: None
77+
InnerRootNode SourceSection: [15,35]`i*2 for i in (1,2,3)...`
78+
GeneratorReturnTargetNode SourceSection: [15,35]`i*2 for i in (1,2,3)...`
79+
flagSlot: 1
80+
GeneratorForNode SourceSection: [15,35]`i*2 for i in (1,2,3)...`
81+
ExpressionStatementNode SourceSection: [15,18]`i*2`
82+
YieldNode SourceSection: [15,18]`i*2`
83+
flagSlot: 0
84+
MulNodeGen SourceSection: [15,18]`i*2`
85+
ReadGeneratorFrameVariableNodeGen SourceSection: [15,16]`i`
86+
Frame: [0,i,Illegal]
87+
IntegerLiteralNode SourceSection: [17,18]`2`
88+
Value: 2
89+
GeneratorAccessNode SourceSection: None
90+
WriteGeneratorFrameVariableNodeGen SourceSection: None
91+
Identifier: i
92+
Frame: [0,i,Illegal]
93+
ArgumentExpressionNode SourceSection: None
94+
ReadIndexedArgumentNodeGen SourceSection: None
95+
Index: 0
96+
GetNextCached SourceSection: None
97+
LookupAndCallUnaryNodeGen SourceSection: None
98+
Op: __next__
99+
GeneratorAccessNode SourceSection: None
100+
IsBuiltinClassProfile SourceSection: None
101+
GetClassNodeGen SourceSection: None
102+
ReadGeneratorFrameVariableNodeGen SourceSection: None
103+
Frame: [1,<return_val>,Illegal]
104+
BlockNode SourceSection: None
105+
GeneratorAccessNode SourceSection: None
106+
PRaiseNodeGen SourceSection: None
107+
GetIteratorExpressionNodeGen SourceSection: [28,35]`(1,2,3)`
108+
TupleLiteralNode SourceSection: [28,35]`(1,2,3)`
109+
IntegerLiteralNode SourceSection: [29,30]`1`
110+
Value: 1
111+
IntegerLiteralNode SourceSection: [31,32]`2`
112+
Value: 2
113+
IntegerLiteralNode SourceSection: [33,34]`3`
114+
Value: 3
115+
PythonObjectFactoryNodeGen SourceSection: None
116+
PythonObjectFactoryNodeGen SourceSection: None
117+
PythonObjectFactoryNodeGen SourceSection: None

0 commit comments

Comments
 (0)