Skip to content

Commit 9613805

Browse files
committed
Exhaust primitive iterators in 'ForNode'.
1 parent da68079 commit 9613805

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/iterator/IteratorBuiltins.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public Object next(PArrayIterator self,
8585

8686
@Specialization
8787
public int next(PIntegerSequenceIterator self) {
88-
if (!self.stopIterationReached && self.index < self.sequence.length()) {
88+
if (!self.isExhausted() && self.index < self.sequence.length()) {
8989
return self.sequence.getIntItemNormalized(self.index++);
9090
}
91-
self.stopIterationReached = true;
91+
self.setExhausted();
9292
throw raise(StopIteration);
9393
}
9494

@@ -114,19 +114,19 @@ public int next(PRangeReverseIterator self) {
114114

115115
@Specialization
116116
public double next(PDoubleSequenceIterator self) {
117-
if (!self.stopIterationReached && self.index < self.sequence.length()) {
117+
if (!self.isExhausted() && self.index < self.sequence.length()) {
118118
return self.sequence.getDoubleItemNormalized(self.index++);
119119
}
120-
self.stopIterationReached = true;
120+
self.setExhausted();
121121
throw raise(StopIteration);
122122
}
123123

124124
@Specialization
125125
public long next(PLongSequenceIterator self) {
126-
if (!self.stopIterationReached && self.index < self.sequence.length()) {
126+
if (!self.isExhausted() && self.index < self.sequence.length()) {
127127
return self.sequence.getLongItemNormalized(self.index++);
128128
}
129-
self.stopIterationReached = true;
129+
self.setExhausted();
130130
throw raise(StopIteration);
131131
}
132132

@@ -157,10 +157,10 @@ public Object next(PSequenceIterator self,
157157
@Cached("createClassProfile()") ValueProfile sequenceProfile,
158158
@Cached("createGetItem()") SequenceStorageNodes.GetItemNode getItemNode) {
159159
PSequence sequence = sequenceProfile.profile(self.getPSequence());
160-
if (!self.stopIterationReached && self.index < sequence.len()) {
160+
if (!self.isExhausted() && self.index < sequence.len()) {
161161
return getItemNode.execute(sequence.getSequenceStorage(), self.index++);
162162
}
163-
self.stopIterationReached = true;
163+
self.setExhausted();
164164
throw raise(StopIteration);
165165
}
166166

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/iterator/PBuiltinIterator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,20 @@
3333
*/
3434
public abstract class PBuiltinIterator extends PythonBuiltinObject {
3535

36+
private boolean exhausted = false;
37+
3638
public PBuiltinIterator(PythonClass clazz) {
3739
super(clazz);
3840
}
3941

42+
public void setExhausted() {
43+
exhausted = true;
44+
}
45+
46+
public boolean isExhausted() {
47+
return exhausted;
48+
}
49+
4050
@Override
4151
public String toString() {
4252
return "<iterator object at " + hashCode() + ">";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/ForNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected boolean doIntegerIterator(VirtualFrame frame, PIntegerIterator iterato
9595
@Cached("iterator.getClass()") Class<? extends PIntegerIterator> clazz) {
9696
PIntegerIterator profiledIterator = clazz.cast(iterator);
9797
if (!profiledIterator.hasNext()) {
98+
profiledIterator.setExhausted();
9899
return false;
99100
}
100101
((WriteNode) target).doWrite(frame, profiledIterator.next());
@@ -106,6 +107,7 @@ protected boolean doLongIterator(VirtualFrame frame, PLongIterator iterator,
106107
@Cached("iterator.getClass()") Class<? extends PLongIterator> clazz) {
107108
PLongIterator profiledIterator = clazz.cast(iterator);
108109
if (!profiledIterator.hasNext()) {
110+
profiledIterator.setExhausted();
109111
return false;
110112
}
111113
((WriteNode) target).doWrite(frame, profiledIterator.next());
@@ -117,6 +119,7 @@ protected boolean doDoubleIterator(VirtualFrame frame, PDoubleIterator iterator,
117119
@Cached("iterator.getClass()") Class<? extends PDoubleIterator> clazz) {
118120
PDoubleIterator profiledIterator = clazz.cast(iterator);
119121
if (!profiledIterator.hasNext()) {
122+
profiledIterator.setExhausted();
120123
return false;
121124
}
122125
((WriteNode) target).doWrite(frame, profiledIterator.next());

0 commit comments

Comments
 (0)