@@ -91,7 +91,12 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9191 @ GenerateNodeFactory
9292 public abstract static class NextNode extends PythonUnaryBuiltinNode {
9393
94- @ Specialization
94+ @ Specialization (guards = "self.isExhausted()" )
95+ public Object exhausted (@ SuppressWarnings ("unused" ) PBuiltinIterator self ) {
96+ throw raise (StopIteration );
97+ }
98+
99+ @ Specialization (guards = "!self.isExhausted()" )
95100 Object next (VirtualFrame frame , PArrayIterator self ,
96101 @ Cached ("createClassProfile()" ) ValueProfile itemTypeProfile ,
97102 @ Cached ("createNotNormalized()" ) SequenceStorageNodes .GetItemNode getItemNode ,
@@ -102,45 +107,47 @@ Object next(VirtualFrame frame, PArrayIterator self,
102107 // types
103108 return itemTypeProfile .profile (getItemNode .execute (frame , sequenceStorage , self .index ++));
104109 }
110+ self .setExhausted ();
105111 throw raise (StopIteration );
106112 }
107113
108- @ Specialization
114+ @ Specialization ( guards = "!self.isExhausted()" )
109115 int next (PIntegerSequenceIterator self ) {
110- if (! self . isExhausted () && self .index < self .sequence .length ()) {
116+ if (self .index < self .sequence .length ()) {
111117 return self .sequence .getIntItemNormalized (self .index ++);
112118 }
113119 self .setExhausted ();
114120 throw raise (StopIteration );
115121 }
116122
117- @ Specialization
123+ @ Specialization ( guards = "!self.isExhausted()" )
118124 int next (PRangeIterator self ) {
119125 if (self .hasNext ()) {
120126 return self .next ();
121127 }
128+ self .setExhausted ();
122129 throw raise (StopIteration );
123130 }
124131
125- @ Specialization
132+ @ Specialization ( guards = "!self.isExhausted()" )
126133 double next (PDoubleSequenceIterator self ) {
127- if (! self . isExhausted () && self .index < self .sequence .length ()) {
134+ if (self .index < self .sequence .length ()) {
128135 return self .sequence .getDoubleItemNormalized (self .index ++);
129136 }
130137 self .setExhausted ();
131138 throw raise (StopIteration );
132139 }
133140
134- @ Specialization
141+ @ Specialization ( guards = "!self.isExhausted()" )
135142 long next (PLongSequenceIterator self ) {
136- if (! self . isExhausted () && self .index < self .sequence .length ()) {
143+ if (self .index < self .sequence .length ()) {
137144 return self .sequence .getLongItemNormalized (self .index ++);
138145 }
139146 self .setExhausted ();
140147 throw raise (StopIteration );
141148 }
142149
143- @ Specialization
150+ @ Specialization ( guards = "!self.isExhausted()" )
144151 public Object next (PBaseSetIterator self ,
145152 @ Cached ("createBinaryProfile()" ) ConditionProfile sizeChanged ,
146153 @ CachedLibrary (limit = "1" ) HashingStorageLibrary storageLibrary ) {
@@ -151,31 +158,33 @@ public Object next(PBaseSetIterator self,
151158 }
152159 return iterator .next ();
153160 }
161+ self .setExhausted ();
154162 throw raise (StopIteration );
155163 }
156164
157- @ Specialization (guards = {"self.isPSequence()" })
165+ @ Specialization (guards = {"self.isPSequence()" , "!self.isExhausted()" })
158166 public Object next (VirtualFrame frame , PSequenceIterator self ,
159167 @ Cached SequenceNodes .GetSequenceStorageNode getStorage ,
160168 @ Cached SequenceStorageNodes .LenNode lenNode ,
161169 @ Cached ("createNotNormalized()" ) SequenceStorageNodes .GetItemNode getItemNode ) {
162170 SequenceStorage s = getStorage .execute (self .getPSequence ());
163- if (! self . isExhausted () && self .index < lenNode .execute (s )) {
171+ if (self .index < lenNode .execute (s )) {
164172 return getItemNode .execute (frame , s , self .index ++);
165173 }
166174 self .setExhausted ();
167175 throw raise (StopIteration );
168176 }
169177
170- @ Specialization
178+ @ Specialization ( guards = "!self.isExhausted()" )
171179 public Object next (PStringIterator self ) {
172180 if (self .index < self .value .length ()) {
173181 return Character .toString (self .value .charAt (self .index ++));
174182 }
183+ self .setExhausted ();
175184 throw raise (StopIteration );
176185 }
177186
178- @ Specialization
187+ @ Specialization ( guards = "!self.isExhausted()" )
179188 public Object next (PDictView .PBaseDictIterator <?> self ,
180189 @ Cached ConditionProfile sizeChanged ,
181190 @ CachedLibrary (limit = "3" ) HashingStorageLibrary storageLibrary ,
@@ -186,6 +195,7 @@ public Object next(PDictView.PBaseDictIterator<?> self,
186195 }
187196 return self .next (factory ());
188197 }
198+ self .setExhausted ();
189199 throw raise (PythonErrorType .StopIteration );
190200 }
191201
@@ -197,6 +207,7 @@ public Object next(VirtualFrame frame, PSequenceIterator self,
197207 return callGetItem .executeObject (frame , self .getObject (), self .index ++);
198208 } catch (PException e ) {
199209 e .expectIndexError (profile );
210+ self .setExhausted ();
200211 throw raise (StopIteration );
201212 }
202213 }
@@ -329,7 +340,7 @@ public Object reduceNonSeq(VirtualFrame frame, PSequenceIterator self,
329340 @ CachedContext (PythonLanguage .class ) PythonContext context ,
330341 @ Cached ("create(__REDUCE__)" ) LookupAndCallUnaryNode callUnaryNode ,
331342 @ Cached .Shared ("pol" ) @ CachedLibrary (limit = "1" ) PythonObjectLibrary pol ) {
332- Object reduce = pol .lookupAttribute (self .getPSequence (), __REDUCE__ );
343+ Object reduce = pol .lookupAttribute (self .getObject (), __REDUCE__ );
333344 Object content = callUnaryNode .executeObject (frame , reduce );
334345 return reduceInternal (content , self .index , context , pol );
335346 }
0 commit comments