Skip to content

Commit 4a5784e

Browse files
committed
iterables passed to permutations might have no __len__
1 parent e4be725 commit 4a5784e

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ItertoolsModuleBuiltins.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import com.oracle.graal.python.builtins.modules.BuiltinFunctions.IterNode;
5353
import com.oracle.graal.python.builtins.objects.PNone;
5454
import com.oracle.graal.python.builtins.objects.function.PKeyword;
55-
import com.oracle.graal.python.builtins.objects.iterator.IteratorNodes.ToArrayNode;
5655
import com.oracle.graal.python.builtins.objects.itertools.PAccumulate;
5756
import com.oracle.graal.python.builtins.objects.itertools.PChain;
5857
import com.oracle.graal.python.builtins.objects.itertools.PCombinations;
@@ -77,9 +76,10 @@
7776
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
7877
import com.oracle.graal.python.lib.PyObjectGetIter;
7978
import com.oracle.graal.python.lib.PyObjectLookupAttr;
80-
import com.oracle.graal.python.lib.PyObjectSizeNode;
8179
import com.oracle.graal.python.lib.PyObjectTypeCheck;
8280
import com.oracle.graal.python.nodes.ErrorMessages;
81+
import com.oracle.graal.python.builtins.objects.iterator.IteratorNodes.ToArrayNode;
82+
import com.oracle.graal.python.lib.PyObjectSizeNode;
8383
import com.oracle.graal.python.nodes.builtins.ListNodes.FastConstructListNode;
8484
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
8585
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -574,18 +574,18 @@ protected ArgumentClinicProvider getArgumentClinic() {
574574

575575
@Specialization(guards = {"isTypeNode.execute(cls)", "isNone(r)"})
576576
Object constructNoR(VirtualFrame frame, Object cls, Object iterable, @SuppressWarnings("unused") PNone r,
577-
@Cached PyObjectSizeNode sizeNode,
577+
@Cached ToArrayNode toArrayNode,
578578
@Cached ConditionProfile nrProfile,
579579
@Cached LoopConditionProfile indicesLoopProfile,
580580
@Cached LoopConditionProfile cyclesLoopProfile,
581581
@SuppressWarnings("unused") @Cached IsTypeNode isTypeNode) {
582-
int len = sizeNode.execute(frame, iterable);
583-
return construct(cls, iterable, len, len, nrProfile, indicesLoopProfile, cyclesLoopProfile);
582+
Object[] pool = toArrayNode.execute(frame, iterable);
583+
return construct(cls, pool, pool.length, nrProfile, indicesLoopProfile, cyclesLoopProfile);
584584
}
585585

586586
@Specialization(guards = {"isTypeNode.execute(cls)", "!isNone(rArg)"})
587587
Object construct(VirtualFrame frame, Object cls, Object iterable, Object rArg,
588-
@Cached PyObjectSizeNode sizeNode,
588+
@Cached ToArrayNode toArrayNode,
589589
@Cached CastToJavaIntExactNode castToInt,
590590
@Cached BranchProfile wrongRprofile,
591591
@Cached BranchProfile negRprofile,
@@ -604,15 +604,15 @@ Object construct(VirtualFrame frame, Object cls, Object iterable, Object rArg,
604604
negRprofile.enter();
605605
throw raise(ValueError, MUST_BE_NON_NEGATIVE, "r");
606606
}
607-
// XXX could be generator
608-
int len = sizeNode.execute(frame, iterable);
609-
return construct(cls, iterable, r, len, nrProfile, indicesLoopProfile, cyclesLoopProfile);
607+
Object[] pool = toArrayNode.execute(frame, iterable);
608+
return construct(cls, pool, r, nrProfile, indicesLoopProfile, cyclesLoopProfile);
610609
}
611610

612-
public PPermutations construct(Object cls, Object iterable, int r, int n, ConditionProfile nrProfile, LoopConditionProfile indicesLoopProfile, LoopConditionProfile cyclesLoopProfile) {
611+
public PPermutations construct(Object cls, Object[] pool, int r, ConditionProfile nrProfile, LoopConditionProfile indicesLoopProfile, LoopConditionProfile cyclesLoopProfile) {
613612
PPermutations self = factory().createPermutations(cls);
614-
self.setPool(iterable);
613+
self.setPool(pool);
615614
self.setR(r);
615+
int n = pool.length;
616616
self.setN(n);
617617
int nMinusR = n - r;
618618
if (nrProfile.profile(nMinusR < 0)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/PPermutations.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import com.oracle.truffle.api.object.Shape;
4545

4646
public final class PPermutations extends PythonBuiltinObject {
47-
private Object pool;
47+
private Object[] pool;
4848
private int r;
4949
private int n;
5050
private boolean stopped;
@@ -57,11 +57,11 @@ public PPermutations(Object cls, Shape instanceShape) {
5757
super(cls, instanceShape);
5858
}
5959

60-
public Object getPool() {
60+
public Object[] getPool() {
6161
return pool;
6262
}
6363

64-
public void setPool(Object pool) {
64+
public void setPool(Object[] pool) {
6565
this.pool = pool;
6666
}
6767

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/itertools/PermutationsBuiltins.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5656
import com.oracle.graal.python.builtins.PythonBuiltins;
5757
import com.oracle.graal.python.builtins.objects.PNone;
58+
import com.oracle.graal.python.builtins.objects.list.PList;
5859
import com.oracle.graal.python.builtins.objects.object.PythonObject;
5960
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6061
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.GetItemNode;
61-
import com.oracle.graal.python.lib.PyObjectGetItem;
6262
import com.oracle.graal.python.lib.PyObjectSizeNode;
6363
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6464
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
@@ -100,8 +100,7 @@ Object next(PPermutations self) {
100100
}
101101

102102
@Specialization(guards = "!self.isStopped()")
103-
Object next(VirtualFrame frame, PPermutations self,
104-
@Cached PyObjectGetItem getItemNode,
103+
Object next(PPermutations self,
105104
@Cached ConditionProfile isStartedProfile,
106105
@Cached BranchProfile jProfile,
107106
@Cached LoopConditionProfile resultLoopProfile,
@@ -111,10 +110,10 @@ Object next(VirtualFrame frame, PPermutations self,
111110

112111
int[] indices = self.getIndices();
113112
Object[] result = new Object[r];
114-
Object pool = self.getPool();
113+
Object[] pool = self.getPool();
115114
resultLoopProfile.profileCounted(r);
116115
for (int i = 0; resultLoopProfile.inject(i < r); i++) {
117-
result[i] = getItemNode.execute(frame, pool, indices[i]);
116+
result[i] = pool[indices[i]];
118117
}
119118

120119
int[] cycles = self.getCycles();
@@ -158,7 +157,8 @@ public abstract static class ReduceNode extends PythonUnaryBuiltinNode {
158157
Object reduce(PPermutations self,
159158
@Cached GetClassNode getClassNode) {
160159
Object type = getClassNode.execute(self);
161-
PTuple tuple = factory().createTuple(new Object[]{self.getPool(), self.getR()});
160+
PList poolList = factory().createList(self.getPool());
161+
PTuple tuple = factory().createTuple(new Object[]{poolList, self.getR()});
162162

163163
// we must pickle the indices and use them for setstate
164164
PTuple indicesTuple = factory().createTuple(self.getIndices());
@@ -198,7 +198,7 @@ Object setState(VirtualFrame frame, PPermutations self, Object state,
198198
}
199199
Object indices = getItemNode.execute(frame, state, 0);
200200
Object cycles = getItemNode.execute(frame, state, 1);
201-
int poolLen = sizeNode.execute(frame, self.getPool());
201+
int poolLen = self.getPool().length;
202202
if (sizeNode.execute(frame, indices) != poolLen || sizeNode.execute(frame, cycles) != self.getR()) {
203203
wrongValuesSizeProfile.enter();
204204
throw raise(ValueError, INVALID_ARGS, __SETSTATE__);

0 commit comments

Comments
 (0)