|
25 | 25 | */
|
26 | 26 | package com.oracle.graal.python.builtins.modules;
|
27 | 27 |
|
28 |
| -import java.util.Collections; |
| 28 | +import static com.oracle.graal.python.nodes.ErrorMessages.S_MUST_BE_S; |
| 29 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError; |
| 30 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__COPY__; |
| 31 | + |
| 32 | +import com.oracle.graal.python.annotations.ArgumentClinic; |
| 33 | +import com.oracle.graal.python.builtins.Builtin; |
29 | 34 | import java.util.List;
|
30 | 35 |
|
31 | 36 | import com.oracle.graal.python.builtins.CoreFunctions;
|
| 37 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
32 | 38 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
| 39 | +import com.oracle.graal.python.builtins.modules.BuiltinFunctions.IterNode; |
| 40 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 41 | +import com.oracle.graal.python.builtins.objects.function.PKeyword; |
| 42 | +import com.oracle.graal.python.builtins.objects.itertools.PChain; |
| 43 | +import com.oracle.graal.python.builtins.objects.itertools.PRepeat; |
| 44 | +import com.oracle.graal.python.builtins.objects.itertools.PTeeDataObject; |
| 45 | +import com.oracle.graal.python.builtins.objects.type.TypeNodes; |
| 46 | +import com.oracle.graal.python.lib.PyCallableCheckNode; |
| 47 | +import com.oracle.graal.python.lib.PyObjectLookupAttr; |
| 48 | +import com.oracle.graal.python.nodes.ErrorMessages; |
| 49 | +import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode; |
33 | 50 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
| 51 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode; |
| 52 | +import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode; |
| 53 | +import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; |
| 54 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
| 55 | +import com.oracle.graal.python.util.PythonUtils; |
| 56 | +import com.oracle.truffle.api.dsl.Cached; |
| 57 | +import com.oracle.truffle.api.dsl.Fallback; |
| 58 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
34 | 59 | import com.oracle.truffle.api.dsl.NodeFactory;
|
| 60 | +import com.oracle.truffle.api.dsl.Specialization; |
| 61 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 62 | +import com.oracle.truffle.api.profiles.BranchProfile; |
35 | 63 |
|
36 | 64 | @CoreFunctions(defineModule = "itertools")
|
37 | 65 | public final class ItertoolsModuleBuiltins extends PythonBuiltins {
|
38 | 66 |
|
39 | 67 | @Override
|
40 | 68 | protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
|
41 |
| - return Collections.emptyList(); |
| 69 | + return ItertoolsModuleBuiltinsFactory.getFactories(); |
| 70 | + } |
| 71 | + |
| 72 | + @Builtin(name = "tee", minNumOfPositionalArgs = 1, parameterNames = {"iterable", "n"}) |
| 73 | + @ArgumentClinic(name = "n", conversion = ArgumentClinic.ClinicConversion.Int, defaultValue = "2") |
| 74 | + @GenerateNodeFactory |
| 75 | + public abstract static class TeeNode extends PythonBinaryClinicBuiltinNode { |
| 76 | + @Override |
| 77 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 78 | + return ItertoolsModuleBuiltinsClinicProviders.TeeNodeClinicProviderGen.INSTANCE; |
| 79 | + } |
| 80 | + |
| 81 | + @SuppressWarnings("unused") |
| 82 | + @Specialization(guards = "n < 0") |
| 83 | + protected Object negativeN(Object iterable, int n) { |
| 84 | + throw raise(ValueError, S_MUST_BE_S, "n", ">=0"); |
| 85 | + } |
| 86 | + |
| 87 | + @SuppressWarnings("unused") |
| 88 | + @Specialization(guards = "n == 0") |
| 89 | + protected Object zeroN(Object iterable, int n) { |
| 90 | + return factory().createTuple(PythonUtils.EMPTY_OBJECT_ARRAY); |
| 91 | + } |
| 92 | + |
| 93 | + @Specialization(guards = "n > 0") |
| 94 | + protected Object tee(VirtualFrame frame, Object iterable, int n, |
| 95 | + @Cached IterNode iterNode, |
| 96 | + @Cached PyObjectLookupAttr getAttrNode, |
| 97 | + @Cached PyCallableCheckNode callableCheckNode, |
| 98 | + @Cached CallVarargsMethodNode callNode, |
| 99 | + @Cached BranchProfile notCallableProfile) { |
| 100 | + Object it = iterNode.execute(frame, iterable, PNone.NO_VALUE); |
| 101 | + Object copyCallable = getAttrNode.execute(frame, it, __COPY__); |
| 102 | + if (!callableCheckNode.execute(copyCallable)) { |
| 103 | + notCallableProfile.enter(); |
| 104 | + // as in Tee.__NEW__() |
| 105 | + PTeeDataObject dataObj = factory().createTeeDataObject(it); |
| 106 | + it = factory().createTee(dataObj, 0); |
| 107 | + } |
| 108 | + |
| 109 | + // return tuple([it] + [it.__copy__() for i in range(1, n)]) |
| 110 | + Object[] tupleObjs = new Object[n]; |
| 111 | + tupleObjs[0] = it; |
| 112 | + |
| 113 | + copyCallable = getAttrNode.execute(frame, it, __COPY__); |
| 114 | + for (int i = 1; i < n; i++) { |
| 115 | + tupleObjs[i] = callNode.execute(frame, copyCallable, PythonUtils.EMPTY_OBJECT_ARRAY, PKeyword.EMPTY_KEYWORDS); |
| 116 | + } |
| 117 | + return factory().createTuple(tupleObjs); |
| 118 | + } |
| 119 | + |
42 | 120 | }
|
| 121 | + |
| 122 | + @Builtin(name = "_tee_dataobject", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PTeeDataObject) |
| 123 | + @GenerateNodeFactory |
| 124 | + public abstract static class TeeDataObjectNode extends PythonVarargsBuiltinNode { |
| 125 | + @SuppressWarnings("unused") |
| 126 | + @Specialization(guards = "isTypeNode.execute(cls)", limit = "1") |
| 127 | + protected PTeeDataObject construct(Object cls, Object[] arguments, PKeyword[] keywords, |
| 128 | + @SuppressWarnings("unused") @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 129 | + return factory().createTeeDataObject(); |
| 130 | + } |
| 131 | + |
| 132 | + @Fallback |
| 133 | + @SuppressWarnings("unused") |
| 134 | + protected Object notype(Object cls, Object[] arguments, PKeyword[] keywords, |
| 135 | + @SuppressWarnings("unused") @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 136 | + throw raise(TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Builtin(name = "repeat", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PRepeat, doc = "repeat(object [,times]) -> create an iterator which returns the object\n" + |
| 141 | + "for the specified number of times. If not specified, returns the object\nendlessly.") |
| 142 | + @GenerateNodeFactory |
| 143 | + public abstract static class RepeatNode extends PythonVarargsBuiltinNode { |
| 144 | + @SuppressWarnings("unused") |
| 145 | + @Specialization(guards = "isTypeNode.execute(cls)", limit = "1") |
| 146 | + protected PRepeat construct(Object cls, Object[] arguments, PKeyword[] keywords, |
| 147 | + @SuppressWarnings("unused") @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 148 | + return factory().createRepeat(); |
| 149 | + } |
| 150 | + |
| 151 | + @Fallback |
| 152 | + @SuppressWarnings("unused") |
| 153 | + protected Object notype(Object cls, Object[] arguments, PKeyword[] keywords) { |
| 154 | + throw raise(TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Builtin(name = "chain", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PChain, doc = "Return a chain object whose .__next__() method returns elements from the " + |
| 159 | + "first iterable until it is exhausted, then elements from the next iterable, until all of the iterables are exhausted.") |
| 160 | + @GenerateNodeFactory |
| 161 | + public abstract static class ChainNode extends PythonVarargsBuiltinNode { |
| 162 | + @SuppressWarnings("unused") |
| 163 | + @Specialization(guards = "isTypeNode.execute(cls)", limit = "1") |
| 164 | + protected PChain construct(Object cls, Object[] arguments, PKeyword[] keywords, |
| 165 | + @SuppressWarnings("unused") @Cached TypeNodes.IsTypeNode isTypeNode) { |
| 166 | + return factory().createChain(); |
| 167 | + } |
| 168 | + |
| 169 | + @Fallback |
| 170 | + @SuppressWarnings("unused") |
| 171 | + protected Object notype(Object cls, Object[] arguments, PKeyword[] keywords) { |
| 172 | + throw raise(TypeError, ErrorMessages.IS_NOT_TYPE_OBJ, "'cls'", cls); |
| 173 | + } |
| 174 | + } |
| 175 | + |
43 | 176 | }
|
0 commit comments