112
112
import com .oracle .truffle .api .dsl .TypeSystemReference ;
113
113
import com .oracle .truffle .api .frame .VirtualFrame ;
114
114
import com .oracle .truffle .api .library .CachedLibrary ;
115
+ import com .oracle .truffle .api .nodes .UnexpectedResultException ;
115
116
import com .oracle .truffle .api .profiles .ConditionProfile ;
116
117
117
118
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PList )
@@ -217,24 +218,26 @@ PNone listRange(PList list, PRange range) {
217
218
return PNone .NONE ;
218
219
}
219
220
220
- @ Specialization (guards = "iterable.isPRangeIterator()" )
221
+ @ Specialization (guards = "iterable.isPRangeIterator()" , rewriteOn = UnexpectedResultException . class )
221
222
PNone listPGenerator (VirtualFrame frame , PList list , PGenerator iterable ,
222
223
@ Cached GetIteratorNode getIteratorNode ,
223
224
@ Cached SequenceStorageNodes .AppendNode appendNode ,
224
225
@ Cached GetNextNode getNextNode ,
225
226
@ Cached IsBuiltinClassProfile errorProfile ,
226
227
@ Cached ("createBinaryProfile()" ) ConditionProfile stepProfile ,
227
- @ Cached ("createBinaryProfile()" ) ConditionProfile positiveRangeProfile ) {
228
+ @ Cached ("createBinaryProfile()" ) ConditionProfile positiveRangeProfile ) throws UnexpectedResultException {
228
229
clearStorage (list );
229
230
Object iterObj = getIteratorNode .executeWith (frame , iterable );
230
231
SequenceStorage storage = EmptySequenceStorage .INSTANCE ;
231
232
232
233
PRangeIterator range = (PRangeIterator ) iterable .getIterator ();
233
234
final int estimatedMaxLen = range .getLength (stepProfile , positiveRangeProfile );
235
+ int realLen = 0 ;
234
236
if (estimatedMaxLen > 0 ) {
235
237
Object value = null ;
236
238
try {
237
239
value = getNextNode .execute (frame , iterObj );
240
+ realLen ++;
238
241
} catch (PException e ) {
239
242
e .expectStopIteration (errorProfile );
240
243
}
@@ -244,6 +247,7 @@ PNone listPGenerator(VirtualFrame frame, PList list, PGenerator iterable,
244
247
while (true ) {
245
248
try {
246
249
storage = appendNode .execute (storage , getNextNode .execute (frame , iterObj ), ListGeneralizationNode .SUPPLIER );
250
+ realLen ++;
247
251
} catch (PException e ) {
248
252
e .expectStopIteration (errorProfile );
249
253
break ;
@@ -253,7 +257,11 @@ PNone listPGenerator(VirtualFrame frame, PList list, PGenerator iterable,
253
257
}
254
258
255
259
list .setSequenceStorage (storage );
256
- return PNone .NONE ;
260
+ if (realLen == estimatedMaxLen ) {
261
+ return PNone .NONE ;
262
+ } else {
263
+ throw new UnexpectedResultException (PNone .NONE );
264
+ }
257
265
}
258
266
259
267
@ Specialization (guards = {"!isNoValue(iterable)" , "!isString(iterable)" })
0 commit comments