Skip to content

Commit 433a6d7

Browse files
committed
[GR-9860] List Iterator provides values even after StopIteration exception.
1 parent 08dfee5 commit 433a6d7

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ class F(object):
613613
def __iter__(self):
614614
raise KeyboardInterrupt
615615
self.assertRaises(KeyboardInterrupt, list, F())
616-
616+
'''
617617
def test_exhausted_iterator(self):
618618
a = self.type2test([1, 2, 3])
619619
exhit = iter(a)
@@ -624,5 +624,4 @@ def test_exhausted_iterator(self):
624624
self.assertEqual(list(exhit), [])
625625
self.assertEqual(list(empit), [9])
626626
self.assertEqual(a, self.type2test([1, 2, 3, 9]))
627-
'''
628627

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ public int next(PIntArrayIterator self) {
7575

7676
@Specialization
7777
public int next(PIntegerSequenceIterator self) {
78-
if (self.index < self.sequence.length()) {
78+
if (!self.stopIterationReached && self.index < self.sequence.length()) {
7979
return self.sequence.getIntItemNormalized(self.index++);
8080
}
81+
self.stopIterationReached = true;
8182
throw raise(StopIteration);
8283
}
8384

@@ -111,9 +112,10 @@ public double next(PDoubleArrayIterator self) {
111112

112113
@Specialization
113114
public double next(PDoubleSequenceIterator self) {
114-
if (self.index < self.sequence.length()) {
115+
if (!self.stopIterationReached && self.index < self.sequence.length()) {
115116
return self.sequence.getDoubleItemNormalized(self.index++);
116117
}
118+
self.stopIterationReached = true;
117119
throw raise(StopIteration);
118120
}
119121

@@ -127,9 +129,10 @@ public long next(PLongArrayIterator self) {
127129

128130
@Specialization
129131
public long next(PLongSequenceIterator self) {
130-
if (self.index < self.sequence.length()) {
132+
if (!self.stopIterationReached && self.index < self.sequence.length()) {
131133
return self.sequence.getLongItemNormalized(self.index++);
132134
}
135+
self.stopIterationReached = true;
133136
throw raise(StopIteration);
134137
}
135138

@@ -143,9 +146,10 @@ public Object next(PBaseSetIterator self) {
143146

144147
@Specialization(guards = "self.isPSequence()")
145148
public Object next(PSequenceIterator self) {
146-
if (self.index < self.getPSequence().len()) {
149+
if (!self.stopIterationReached && self.index < self.getPSequence().len()) {
147150
return self.getPSequence().getItem(self.index++);
148151
}
152+
self.stopIterationReached = true;
149153
throw raise(StopIteration);
150154
}
151155

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public final class PDoubleSequenceIterator extends PDoubleIterator {
3232

3333
final DoubleSequenceStorage sequence;
3434
int index;
35+
protected boolean stopIterationReached = false;
3536

3637
public PDoubleSequenceIterator(PythonClass clazz, DoubleSequenceStorage sequence) {
3738
super(clazz);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public final class PIntegerSequenceIterator extends PIntegerIterator {
3232

3333
final IntSequenceStorage sequence;
3434
int index;
35+
protected boolean stopIterationReached = false;
3536

3637
public PIntegerSequenceIterator(PythonClass clazz, IntSequenceStorage sequence) {
3738
super(clazz);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public final class PLongSequenceIterator extends PLongIterator {
3232

3333
final LongSequenceStorage sequence;
3434
int index;
35-
35+
protected boolean stopIterationReached = false;
36+
3637
public PLongSequenceIterator(PythonClass clazz, LongSequenceStorage sequence) {
3738
super(clazz);
3839
this.sequence = sequence;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
public final class PSequenceIterator extends PBuiltinIterator {
3232
protected final Object sequence;
3333
protected int index = 0;
34+
protected boolean stopIterationReached = false;
3435

3536
public PSequenceIterator(PythonClass clazz, Object sequence) {
3637
super(clazz);

0 commit comments

Comments
 (0)