Skip to content

Commit 95834c4

Browse files
author
Franziska Geiger
committed
[GR-17191] Add iterator roll out to DestructuringAssignmentNode
PullRequest: graalpython/593
2 parents b57d855 + 0f07f48 commit 95834c4

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,19 @@ def test_else_break_from_while():
112112
iters += 1
113113
break
114114
assert iters == 11, "if the while-loop doesn't break, the else should be executed and break out of the outer loop"
115+
116+
class A(object):
117+
def __iter__(self):
118+
return iter(['key', 'value'])
119+
120+
121+
def test_for_iter():
122+
a1 = A()
123+
a2 = A()
124+
list = [a1, a2]
125+
iterator = 0
126+
for key, value in list:
127+
assert 'key' in key
128+
assert 'value' in value
129+
iterator += 1
130+
assert iterator==2

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DestructuringAssignmentNode.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory;
3838
import com.oracle.graal.python.nodes.PRaiseNode;
3939
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
40+
import com.oracle.graal.python.nodes.builtins.ListNodes.CreateStorageFromIteratorNode;
4041
import com.oracle.graal.python.nodes.builtins.TupleNodes;
42+
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
4143
import com.oracle.graal.python.nodes.expression.ExpressionNode;
4244
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4345
import com.oracle.graal.python.nodes.statement.StatementNode;
@@ -54,7 +56,7 @@
5456
import com.oracle.truffle.api.profiles.BranchProfile;
5557

5658
public final class DestructuringAssignmentNode extends StatementNode implements WriteNode {
57-
@Child private PythonObjectFactory factory;
59+
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
5860
@Child private PRaiseNode raiseNode;
5961
@CompilationFinal private ContextReference<PythonContext> contextRef;
6062

@@ -68,6 +70,9 @@ public final class DestructuringAssignmentNode extends StatementNode implements
6870
@Child private LookupInheritedAttributeNode lookupGetItemNode = LookupInheritedAttributeNode.create(__GETITEM__);
6971
@Child private TupleNodes.ConstructTupleNode constructTupleNode = TupleNodes.ConstructTupleNode.create();
7072

73+
@Child private CreateStorageFromIteratorNode storageNode = CreateStorageFromIteratorNode.create();
74+
@Child private GetIteratorNode getIteratorNode = GetIteratorNode.create();
75+
7176
private final IsBuiltinClassProfile notEnoughValuesProfile = IsBuiltinClassProfile.create();
7277
private final IsBuiltinClassProfile tooManyValuesErrorProfile = IsBuiltinClassProfile.create();
7378
private final BranchProfile tooManyValuesProfile = BranchProfile.create();
@@ -120,10 +125,6 @@ private int fillFromArray(VirtualFrame frame, Object[] array, int startIndex) {
120125

121126
private int fillStarred(VirtualFrame frame, Object rhsValue) {
122127
int pos = starredIndex;
123-
if (factory == null) {
124-
CompilerDirectives.transferToInterpreterAndInvalidate();
125-
factory = insert(PythonObjectFactory.create());
126-
}
127128
try {
128129
// TODO(ls): proper cast to int
129130
// TODO(ls): the result of the len call doesn't seem to be used in Python
@@ -175,7 +176,8 @@ public ExpressionNode getRhs() {
175176
return rhs;
176177
}
177178

178-
public void doWrite(VirtualFrame frame, Object rhsValue) {
179+
public void doWrite(VirtualFrame frame, Object rhsVal) {
180+
Object rhsValue = factory.createList(storageNode.execute(frame, getIteratorNode.executeWith(frame, rhsVal)));
179181
int nonExistingItem;
180182
try {
181183
if (starredIndex == -1) {

0 commit comments

Comments
 (0)