Skip to content

Commit f19919a

Browse files
committed
Rewrite when the estimated list size didn't match
1 parent 175cfd0 commit f19919a

File tree

1 file changed

+11
-3
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list

1 file changed

+11
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
import com.oracle.truffle.api.dsl.TypeSystemReference;
113113
import com.oracle.truffle.api.frame.VirtualFrame;
114114
import com.oracle.truffle.api.library.CachedLibrary;
115+
import com.oracle.truffle.api.nodes.UnexpectedResultException;
115116
import com.oracle.truffle.api.profiles.ConditionProfile;
116117

117118
@CoreFunctions(extendClasses = PythonBuiltinClassType.PList)
@@ -217,24 +218,26 @@ PNone listRange(PList list, PRange range) {
217218
return PNone.NONE;
218219
}
219220

220-
@Specialization(guards = "iterable.isPRangeIterator()")
221+
@Specialization(guards = "iterable.isPRangeIterator()", rewriteOn = UnexpectedResultException.class)
221222
PNone listPGenerator(VirtualFrame frame, PList list, PGenerator iterable,
222223
@Cached GetIteratorNode getIteratorNode,
223224
@Cached SequenceStorageNodes.AppendNode appendNode,
224225
@Cached GetNextNode getNextNode,
225226
@Cached IsBuiltinClassProfile errorProfile,
226227
@Cached("createBinaryProfile()") ConditionProfile stepProfile,
227-
@Cached("createBinaryProfile()") ConditionProfile positiveRangeProfile) {
228+
@Cached("createBinaryProfile()") ConditionProfile positiveRangeProfile) throws UnexpectedResultException {
228229
clearStorage(list);
229230
Object iterObj = getIteratorNode.executeWith(frame, iterable);
230231
SequenceStorage storage = EmptySequenceStorage.INSTANCE;
231232

232233
PRangeIterator range = (PRangeIterator) iterable.getIterator();
233234
final int estimatedMaxLen = range.getLength(stepProfile, positiveRangeProfile);
235+
int realLen = 0;
234236
if (estimatedMaxLen > 0) {
235237
Object value = null;
236238
try {
237239
value = getNextNode.execute(frame, iterObj);
240+
realLen++;
238241
} catch (PException e) {
239242
e.expectStopIteration(errorProfile);
240243
}
@@ -244,6 +247,7 @@ PNone listPGenerator(VirtualFrame frame, PList list, PGenerator iterable,
244247
while (true) {
245248
try {
246249
storage = appendNode.execute(storage, getNextNode.execute(frame, iterObj), ListGeneralizationNode.SUPPLIER);
250+
realLen++;
247251
} catch (PException e) {
248252
e.expectStopIteration(errorProfile);
249253
break;
@@ -253,7 +257,11 @@ PNone listPGenerator(VirtualFrame frame, PList list, PGenerator iterable,
253257
}
254258

255259
list.setSequenceStorage(storage);
256-
return PNone.NONE;
260+
if (realLen == estimatedMaxLen) {
261+
return PNone.NONE;
262+
} else {
263+
throw new UnexpectedResultException(PNone.NONE);
264+
}
257265
}
258266

259267
@Specialization(guards = {"!isNoValue(iterable)", "!isString(iterable)"})

0 commit comments

Comments
 (0)