Skip to content

Commit 4a95eb5

Browse files
committed
Fix iterator __length_hint__
1 parent 374a702 commit 4a95eb5

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5757
import com.oracle.graal.python.nodes.ErrorMessages;
5858
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
59-
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
6059
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6160
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6261
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
@@ -278,43 +277,47 @@ public int lengthHint(PBigRangeIterator self,
278277
}
279278

280279
@Specialization(guards = "!self.isExhausted()")
281-
public double lengthHint(PDoubleSequenceIterator self) {
280+
public int lengthHint(PDoubleSequenceIterator self) {
282281
int len = self.sequence.length() - self.getIndex();
283282
return len < 0 ? 0 : len;
284283
}
285284

286285
@Specialization(guards = "!self.isExhausted()")
287-
public long lengthHint(PLongSequenceIterator self) {
286+
public int lengthHint(PLongSequenceIterator self) {
288287
int len = self.sequence.length() - self.getIndex();
289288
return len < 0 ? 0 : len;
290289
}
291290

292291
@Specialization(guards = "!self.isExhausted()")
293-
public long lengthHint(PBaseSetIterator self,
292+
public int lengthHint(PBaseSetIterator self,
294293
@Cached ConditionProfile profile) {
295294
int size = self.getSize();
296295
if (profile.profile(self.getSet().size() != size)) {
297296
return 0;
298297
}
299-
return size - self.getIndex();
298+
int len = size - self.getIndex();
299+
return len < 0 ? 0 : len;
300300
}
301301

302302
@Specialization(guards = "!self.isExhausted()")
303-
public Object lengthHint(PStringIterator self) {
304-
return self.value.length() - self.getIndex();
303+
public int lengthHint(PStringIterator self) {
304+
int len = self.value.length() - self.getIndex();
305+
return len < 0 ? 0 : len;
305306
}
306307

307308
@Specialization(guards = {"!self.isExhausted()", "self.isPSequence()"})
308-
public Object lengthHint(PSequenceIterator self,
309+
public int lengthHint(PSequenceIterator self,
309310
@Cached SequenceNodes.LenNode lenNode) {
310-
return lenNode.execute(self.getPSequence()) - self.getIndex();
311+
int len = lenNode.execute(self.getPSequence()) - self.getIndex();
312+
return len < 0 ? 0 : len;
311313
}
312314

313-
@Specialization(guards = {"!self.isExhausted()", "!self.isPSequence()"})
314-
public Object lengthHint(VirtualFrame frame, PSequenceIterator self,
315-
@Cached("create(__LEN__)") LookupAndCallUnaryNode callLen,
316-
@Cached("create(__SUB__, __RSUB__)") LookupAndCallBinaryNode callSub) {
317-
return callSub.executeObject(frame, callLen.executeObject(frame, self.getObject()), self.getIndex());
315+
@Specialization(guards = {"!self.isExhausted()", "!self.isPSequence()"}, limit = "getCallSiteInlineCacheMaxDepth()")
316+
public int lengthHint(VirtualFrame frame, PSequenceIterator self,
317+
@CachedLibrary("self.getObject()") PythonObjectLibrary lib,
318+
@Cached ConditionProfile hasFrame) {
319+
int len = lib.lengthWithFrame(self.getObject(), hasFrame, frame) - self.getIndex();
320+
return len < 0 ? 0 : len;
318321
}
319322
}
320323

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/reversed/ReversedBuiltins.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.oracle.truffle.api.dsl.Specialization;
6060
import com.oracle.truffle.api.frame.VirtualFrame;
6161
import com.oracle.truffle.api.library.CachedLibrary;
62+
import com.oracle.truffle.api.profiles.ConditionProfile;
6263

6364
@CoreFunctions(extendClasses = PythonBuiltinClassType.PReverseIterator)
6465
public class ReversedBuiltins extends PythonBuiltins {
@@ -132,7 +133,7 @@ public int lengthHint(PStringReverseIterator self) {
132133
}
133134

134135
@Specialization(guards = {"!self.isExhausted()", "self.isPSequence()"})
135-
public Object lengthHint(PSequenceReverseIterator self,
136+
public int lengthHint(PSequenceReverseIterator self,
136137
@Cached SequenceNodes.LenNode lenNode) {
137138
int len = lenNode.execute(self.getPSequence());
138139
if (len == -1) {
@@ -144,15 +145,15 @@ public Object lengthHint(PSequenceReverseIterator self,
144145
return self.index + 1;
145146
}
146147

147-
@Specialization(guards = {"!self.isExhausted()", "!self.isPSequence()"})
148-
public Object lengthHint(VirtualFrame frame, PSequenceReverseIterator self,
149-
@Cached("create(__LEN__)") LookupAndCallUnaryNode callLen,
150-
@Cached("create(__ADD__, __RADD__)") LookupAndCallBinaryNode callAdd) {
151-
Object len = callLen.executeObject(frame, self.getObject());
152-
if (len == PNone.NO_VALUE || len == PNone.NONE) {
153-
throw raise(TypeError, OBJ_HAS_NO_LEN, self);
148+
@Specialization(guards = {"!self.isExhausted()", "!self.isPSequence()"}, limit = "getCallSiteInlineCacheMaxDepth()")
149+
public int lengthHint(VirtualFrame frame, PSequenceReverseIterator self,
150+
@CachedLibrary("self.getObject()") PythonObjectLibrary lib,
151+
@Cached ConditionProfile hasFrame) {
152+
int len = lib.lengthWithFrame(self.getObject(), hasFrame, frame);
153+
if (len < self.index) {
154+
return 0;
154155
}
155-
return callAdd.executeObject(frame, self.index, 1);
156+
return self.index + 1;
156157
}
157158
}
158159

0 commit comments

Comments
 (0)