Skip to content

Commit 3567276

Browse files
msimaceksteve-s
authored andcommittedMar 21, 2025
Use a control flow exception to communicate iterator exhaustion
1 parent fab3eb0 commit 3567276

File tree

80 files changed

+767
-575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+767
-575
lines changed
 

Diff for: ‎graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/datatype/PRangeTests.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.oracle.graal.python.PythonLanguage;
3434
import com.oracle.graal.python.builtins.objects.range.PIntRange;
3535
import com.oracle.graal.python.builtins.objects.range.PRange;
36+
import com.oracle.graal.python.lib.IteratorExhausted;
3637
import com.oracle.graal.python.lib.PyIterNextNode;
3738
import com.oracle.graal.python.lib.PyObjectGetIter;
3839
import com.oracle.graal.python.nodes.PGuards;
@@ -69,13 +70,14 @@ public void loopWithOnlyStop() throws UnexpectedResultException {
6970
Object iter = PyObjectGetIter.executeUncached(range);
7071

7172
while (true) {
72-
Object next = PyIterNextNode.executeUncached(iter);
73-
if (PyIterNextNode.isExhausted(next)) {
73+
try {
74+
Object next = PyIterNextNode.executeUncached(iter);
75+
int item = PGuards.expectInteger(next);
76+
assertEquals(index, item);
77+
index++;
78+
} catch (IteratorExhausted e) {
7479
break;
7580
}
76-
int item = PGuards.expectInteger(next);
77-
assertEquals(index, item);
78-
index++;
7981
}
8082
} finally {
8183
PythonTests.closeContext();
@@ -92,13 +94,14 @@ public void loopWithStep() throws UnexpectedResultException {
9294
Object iter = PyObjectGetIter.executeUncached(range);
9395

9496
while (true) {
95-
Object next = PyIterNextNode.executeUncached(iter);
96-
if (PyIterNextNode.isExhausted(next)) {
97+
try {
98+
Object next = PyIterNextNode.executeUncached(iter);
99+
int item = PGuards.expectInteger(next);
100+
assertEquals(index, item);
101+
index += 2;
102+
} catch (IteratorExhausted e) {
97103
break;
98104
}
99-
int item = PGuards.expectInteger(next);
100-
assertEquals(index, item);
101-
index += 2;
102105
}
103106
} finally {
104107
PythonTests.closeContext();

Diff for: ‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ArrayModuleBuiltins.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.oracle.graal.python.builtins.objects.range.PIntRange;
6262
import com.oracle.graal.python.builtins.objects.str.StringNodes.CastToTruffleStringCheckedNode;
6363
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
64+
import com.oracle.graal.python.lib.IteratorExhausted;
6465
import com.oracle.graal.python.lib.PyIterNextNode;
6566
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
6667
import com.oracle.graal.python.lib.PyObjectGetIter;
@@ -295,18 +296,19 @@ static PArray arrayIteratorInitializer(VirtualFrame frame, Node inliningTarget,
295296

296297
int length = 0;
297298
while (true) {
298-
Object nextValue = nextNode.execute(frame, inliningTarget, iter);
299-
if (PyIterNextNode.isExhausted(nextValue)) {
300-
break;
301-
}
302299
try {
303-
length = PythonUtils.addExact(length, 1);
304-
ensureCapacityNode.execute(inliningTarget, array, length);
305-
} catch (OverflowException e) {
306-
CompilerDirectives.transferToInterpreterAndInvalidate();
307-
throw PRaiseNode.raiseStatic(inliningTarget, MemoryError);
300+
Object nextValue = nextNode.execute(frame, inliningTarget, iter);
301+
try {
302+
length = PythonUtils.addExact(length, 1);
303+
ensureCapacityNode.execute(inliningTarget, array, length);
304+
} catch (OverflowException e) {
305+
CompilerDirectives.transferToInterpreterAndInvalidate();
306+
throw PRaiseNode.raiseStatic(inliningTarget, MemoryError);
307+
}
308+
putValueNode.execute(frame, inliningTarget, array, length - 1, nextValue);
309+
} catch (IteratorExhausted e) {
310+
break;
308311
}
309-
putValueNode.execute(frame, inliningTarget, array, length - 1, nextValue);
310312
}
311313

312314
setLengthNode.execute(inliningTarget, array, length);

0 commit comments

Comments
 (0)