Skip to content

Commit 295ec6a

Browse files
committed
Move all deprecated itertools __setstate__ and __reduce__ builtins behind TruffleBoundary
1 parent 89e7cce commit 295ec6a

23 files changed

+324
-431
lines changed

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@
4242
import com.oracle.graal.python.lib.PyObjectGetIter;
4343
import com.oracle.graal.python.nodes.PRaiseNode;
4444
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
45+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
4546
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
47+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4648
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
49+
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
50+
import com.oracle.graal.python.runtime.IndirectCallData;
4751
import com.oracle.graal.python.runtime.object.PFactory;
4852
import com.oracle.graal.python.util.PythonUtils;
4953
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -64,11 +68,56 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
6468
return ItertoolsModuleBuiltinsFactory.getFactories();
6569
}
6670

67-
@TruffleBoundary
68-
public static void warnPickleDeprecated() {
71+
private static void warnPickleDeprecated() {
6972
WarningsModuleBuiltins.WarnNode.getUncached().warnEx(null, DeprecationWarning, PICKLE_ITERTOOLS_IN_PYTHON_3_14, 1);
7073
}
7174

75+
@SuppressWarnings("this-escape")
76+
public abstract static class DeprecatedReduceBuiltin extends PythonUnaryBuiltinNode {
77+
private final IndirectCallData indirectCallData = IndirectCallData.createFor(this);
78+
79+
@Override
80+
public final Object execute(VirtualFrame frame, Object arg) {
81+
Object saved = IndirectCallContext.enter(frame, this, indirectCallData);
82+
try {
83+
return warnAndExecute(arg);
84+
} finally {
85+
IndirectCallContext.exit(frame, this, indirectCallData, saved);
86+
}
87+
}
88+
89+
@TruffleBoundary
90+
private Object warnAndExecute(Object arg) {
91+
warnPickleDeprecated();
92+
return executeImpl(arg);
93+
}
94+
95+
protected abstract Object executeImpl(Object arg);
96+
}
97+
98+
@SuppressWarnings("this-escape")
99+
public abstract static class DeprecatedSetStateBuiltin extends PythonBinaryBuiltinNode {
100+
private final IndirectCallData indirectCallData = IndirectCallData.createFor(this);
101+
102+
@Override
103+
public final Object execute(VirtualFrame frame, Object arg1, Object arg2) {
104+
Object saved = IndirectCallContext.enter(frame, this, indirectCallData);
105+
try {
106+
return warnAndExecute(arg1, arg2);
107+
} finally {
108+
IndirectCallContext.exit(frame, this, indirectCallData, saved);
109+
}
110+
}
111+
112+
@TruffleBoundary
113+
private Object warnAndExecute(Object arg1, Object arg2) {
114+
warnPickleDeprecated();
115+
return executeImpl(arg1, arg2);
116+
}
117+
118+
protected abstract Object executeImpl(Object arg1, Object arg2);
119+
}
120+
72121
@Builtin(name = "tee", minNumOfPositionalArgs = 1, parameterNames = {"iterable", "n"})
73122
@ArgumentClinic(name = "n", conversion = ArgumentClinic.ClinicConversion.Int, defaultValue = "2")
74123
@GenerateNodeFactory

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

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.itertools;
4242

43-
import static com.oracle.graal.python.builtins.modules.ItertoolsModuleBuiltins.warnPickleDeprecated;
4443
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___REDUCE__;
4544
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___SETSTATE__;
4645

4746
import java.util.List;
4847

4948
import com.oracle.graal.python.PythonLanguage;
49+
import com.oracle.graal.python.annotations.Builtin;
5050
import com.oracle.graal.python.annotations.Slot;
5151
import com.oracle.graal.python.annotations.Slot.SlotKind;
5252
import com.oracle.graal.python.annotations.Slot.SlotSignature;
53-
import com.oracle.graal.python.annotations.Builtin;
5453
import com.oracle.graal.python.builtins.CoreFunctions;
5554
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5655
import com.oracle.graal.python.builtins.PythonBuiltins;
56+
import com.oracle.graal.python.builtins.modules.ItertoolsModuleBuiltins.DeprecatedReduceBuiltin;
57+
import com.oracle.graal.python.builtins.modules.ItertoolsModuleBuiltins.DeprecatedSetStateBuiltin;
5758
import com.oracle.graal.python.builtins.objects.PNone;
5859
import com.oracle.graal.python.builtins.objects.list.PList;
5960
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
@@ -67,7 +68,6 @@
6768
import com.oracle.graal.python.nodes.call.CallNode;
6869
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6970
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
70-
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7171
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7272
import com.oracle.graal.python.nodes.object.GetClassNode;
7373
import com.oracle.graal.python.runtime.object.PFactory;
@@ -157,41 +157,29 @@ static Object next(VirtualFrame frame, PAccumulate self,
157157

158158
@Builtin(name = J___REDUCE__, minNumOfPositionalArgs = 1)
159159
@GenerateNodeFactory
160-
public abstract static class ReduceNode extends PythonUnaryBuiltinNode {
160+
public abstract static class ReduceNode extends DeprecatedReduceBuiltin {
161161

162162
@Specialization
163-
static Object reduceNoFunc(VirtualFrame frame, PAccumulate self,
164-
@Bind Node inliningTarget,
165-
@Cached GetClassNode getClassNode,
166-
@Cached InlinedBranchProfile hasInitialProfile,
167-
@Cached InlinedBranchProfile totalNoneProfile,
168-
@Cached InlinedBranchProfile totalMarkerProfile,
169-
@Cached InlinedBranchProfile elseProfile,
170-
@Cached PyObjectGetIter getIter,
171-
@Bind PythonLanguage language) {
172-
warnPickleDeprecated();
163+
static Object reduceNoFunc(PAccumulate self) {
164+
PythonLanguage language = PythonLanguage.get(null);
173165
Object func = self.getFunc();
174166
if (func == null) {
175167
func = PNone.NONE;
176168
}
177169
if (self.getInitial() != null) {
178-
hasInitialProfile.enter(inliningTarget);
179-
180-
Object type = getClassNode.execute(inliningTarget, self);
170+
Object type = GetClassNode.executeUncached(self);
181171
PChain chain = PFactory.createChain(language);
182-
chain.setSource(getIter.execute(frame, inliningTarget, PFactory.createList(language, new Object[]{self.getIterable()})));
172+
chain.setSource(PyObjectGetIter.executeUncached(PFactory.createList(language, new Object[]{self.getIterable()})));
183173
PTuple initialTuple = PFactory.createTuple(language, new Object[]{self.getInitial()});
184-
chain.setActive(getIter.execute(frame, inliningTarget, initialTuple));
174+
chain.setActive(PyObjectGetIter.executeUncached(initialTuple));
185175

186176
PTuple tuple = PFactory.createTuple(language, new Object[]{chain, func});
187177
return PFactory.createTuple(language, new Object[]{type, tuple, PNone.NONE});
188178
} else if (self.getTotal() == PNone.NONE) {
189-
totalNoneProfile.enter(inliningTarget);
190-
191179
PChain chain = PFactory.createChain(language);
192180
PList noneList = PFactory.createList(language, new Object[]{PNone.NONE});
193-
Object noneIter = getIter.execute(frame, inliningTarget, noneList);
194-
chain.setSource(getIter.execute(frame, inliningTarget, PFactory.createList(language, new Object[]{noneIter, self.getIterable()})));
181+
Object noneIter = PyObjectGetIter.executeUncached(noneList);
182+
chain.setSource(PyObjectGetIter.executeUncached(PFactory.createList(language, new Object[]{noneIter, self.getIterable()})));
195183
chain.setActive(PNone.NONE);
196184
PAccumulate accumulate = PFactory.createAccumulate(language);
197185
accumulate.setIterable(chain);
@@ -200,15 +188,11 @@ static Object reduceNoFunc(VirtualFrame frame, PAccumulate self,
200188
PTuple tuple = PFactory.createTuple(language, new Object[]{accumulate, 1, PNone.NONE});
201189
return PFactory.createTuple(language, new Object[]{PythonBuiltinClassType.PIslice, tuple});
202190
} else if (self.getTotal() != null) {
203-
totalMarkerProfile.enter(inliningTarget);
204-
205-
Object type = getClassNode.execute(inliningTarget, self);
191+
Object type = GetClassNode.executeUncached(self);
206192
PTuple tuple = PFactory.createTuple(language, new Object[]{self.getIterable(), func});
207193
return PFactory.createTuple(language, new Object[]{type, tuple, self.getTotal()});
208194
} else {
209-
elseProfile.enter(inliningTarget);
210-
211-
Object type = getClassNode.execute(inliningTarget, self);
195+
Object type = GetClassNode.executeUncached(self);
212196
PTuple tuple = PFactory.createTuple(language, new Object[]{self.getIterable(), func});
213197
return PFactory.createTuple(language, new Object[]{type, tuple});
214198
}
@@ -218,10 +202,9 @@ static Object reduceNoFunc(VirtualFrame frame, PAccumulate self,
218202

219203
@Builtin(name = J___SETSTATE__, minNumOfPositionalArgs = 2)
220204
@GenerateNodeFactory
221-
public abstract static class SetStateNode extends PythonBinaryBuiltinNode {
205+
public abstract static class SetStateNode extends DeprecatedSetStateBuiltin {
222206
@Specialization
223207
static Object setState(PAccumulate self, Object state) {
224-
warnPickleDeprecated();
225208
self.setTotal(state);
226209
return PNone.NONE;
227210
}

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

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
package com.oracle.graal.python.builtins.objects.itertools;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
44-
import static com.oracle.graal.python.builtins.modules.ItertoolsModuleBuiltins.warnPickleDeprecated;
4544
import static com.oracle.graal.python.nodes.ErrorMessages.ARGUMENTS_MUST_BE_ITERATORS;
4645
import static com.oracle.graal.python.nodes.ErrorMessages.IS_NOT_A;
4746
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___CLASS_GETITEM__;
@@ -51,25 +50,27 @@
5150
import java.util.List;
5251

5352
import com.oracle.graal.python.PythonLanguage;
53+
import com.oracle.graal.python.annotations.Builtin;
5454
import com.oracle.graal.python.annotations.Slot;
5555
import com.oracle.graal.python.annotations.Slot.SlotKind;
5656
import com.oracle.graal.python.annotations.Slot.SlotSignature;
57-
import com.oracle.graal.python.annotations.Builtin;
5857
import com.oracle.graal.python.builtins.CoreFunctions;
5958
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6059
import com.oracle.graal.python.builtins.PythonBuiltins;
60+
import com.oracle.graal.python.builtins.modules.ItertoolsModuleBuiltins.DeprecatedReduceBuiltin;
61+
import com.oracle.graal.python.builtins.modules.ItertoolsModuleBuiltins.DeprecatedSetStateBuiltin;
6162
import com.oracle.graal.python.builtins.objects.PNone;
6263
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6364
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
64-
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.GetItemNode;
65-
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.LenNode;
6665
import com.oracle.graal.python.builtins.objects.type.TpSlots;
6766
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
6867
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotIterNext.TpIterNextBuiltin;
6968
import com.oracle.graal.python.lib.IteratorExhausted;
7069
import com.oracle.graal.python.lib.PyIterCheckNode;
7170
import com.oracle.graal.python.lib.PyIterNextNode;
7271
import com.oracle.graal.python.lib.PyObjectGetIter;
72+
import com.oracle.graal.python.lib.PyTupleGetItem;
73+
import com.oracle.graal.python.lib.PyTupleSizeNode;
7374
import com.oracle.graal.python.nodes.ErrorMessages;
7475
import com.oracle.graal.python.nodes.PRaiseNode;
7576
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -87,7 +88,6 @@
8788
import com.oracle.truffle.api.frame.VirtualFrame;
8889
import com.oracle.truffle.api.nodes.Node;
8990
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
90-
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
9191
import com.oracle.truffle.api.profiles.InlinedLoopConditionProfile;
9292

9393
@CoreFunctions(extendClasses = {PythonBuiltinClassType.PChain})
@@ -195,19 +195,14 @@ static Object fromIter(VirtualFrame frame, @SuppressWarnings("unused") Object cl
195195

196196
@Builtin(name = J___REDUCE__, minNumOfPositionalArgs = 1)
197197
@GenerateNodeFactory
198-
public abstract static class ReduceNode extends PythonUnaryBuiltinNode {
198+
public abstract static class ReduceNode extends DeprecatedReduceBuiltin {
199199
@Specialization
200-
static Object reducePos(PChain self,
201-
@Bind Node inliningTarget,
202-
@Cached GetClassNode getClass,
203-
@Cached InlinedConditionProfile hasSourceProfile,
204-
@Cached InlinedConditionProfile hasActiveProfile,
205-
@Bind PythonLanguage language) {
206-
warnPickleDeprecated();
207-
Object type = getClass.execute(inliningTarget, self);
200+
static Object reducePos(PChain self) {
201+
Object type = GetClassNode.executeUncached(self);
202+
PythonLanguage language = PythonLanguage.get(null);
208203
PTuple empty = PFactory.createEmptyTuple(language);
209-
if (hasSourceProfile.profile(inliningTarget, self.getSource() != PNone.NONE)) {
210-
if (hasActiveProfile.profile(inliningTarget, self.getActive() != PNone.NONE)) {
204+
if (self.getSource() != PNone.NONE) {
205+
if (self.getActive() != PNone.NONE) {
211206
PTuple tuple = PFactory.createTuple(language, new Object[]{self.getSource(), self.getActive()});
212207
return PFactory.createTuple(language, new Object[]{type, empty, tuple});
213208
} else {
@@ -222,38 +217,31 @@ static Object reducePos(PChain self,
222217

223218
@Builtin(name = J___SETSTATE__, minNumOfPositionalArgs = 2)
224219
@GenerateNodeFactory
225-
public abstract static class SetStateNode extends PythonBinaryBuiltinNode {
220+
public abstract static class SetStateNode extends DeprecatedSetStateBuiltin {
226221
@Specialization
227-
static Object setState(VirtualFrame frame, PChain self, Object state,
228-
@Bind Node inliningTarget,
229-
@Cached LenNode lenNode,
230-
@Cached GetItemNode getItemNode,
231-
@Cached InlinedBranchProfile len2Profile,
232-
@Cached PyIterCheckNode iterCheckNode,
233-
@Cached PRaiseNode raiseNode) {
234-
warnPickleDeprecated();
222+
static Object setState(PChain self, Object state,
223+
@Bind Node node) {
235224
if (!(state instanceof PTuple)) {
236-
throw raiseNode.raise(inliningTarget, TypeError, IS_NOT_A, "state", "a length 1 or 2 tuple");
225+
throw PRaiseNode.raiseStatic(node, TypeError, IS_NOT_A, "state", "a length 1 or 2 tuple");
237226
}
238-
int len = (int) lenNode.execute(frame, state);
227+
int len = PyTupleSizeNode.executeUncached(state);
239228
if (len < 1 || len > 2) {
240-
throw raiseNode.raise(inliningTarget, TypeError, IS_NOT_A, "state", "a length 1 or 2 tuple");
229+
throw PRaiseNode.raiseStatic(node, TypeError, IS_NOT_A, "state", "a length 1 or 2 tuple");
241230
}
242-
Object source = getItemNode.execute(frame, state, 0);
243-
checkIterator(inliningTarget, iterCheckNode, source, raiseNode);
231+
Object source = PyTupleGetItem.executeUncached(state, 0);
232+
checkIterator(node, source);
244233
self.setSource(source);
245234
if (len == 2) {
246-
len2Profile.enter(inliningTarget);
247-
Object active = getItemNode.execute(frame, state, 1);
248-
checkIterator(inliningTarget, iterCheckNode, active, raiseNode);
235+
Object active = PyTupleGetItem.executeUncached(state, 1);
236+
checkIterator(node, active);
249237
self.setActive(active);
250238
}
251239
return PNone.NONE;
252240
}
253241

254-
private static void checkIterator(Node inliningTarget, PyIterCheckNode iterCheckNode, Object obj, PRaiseNode raiseNode) throws PException {
255-
if (!iterCheckNode.execute(inliningTarget, obj)) {
256-
throw raiseNode.raise(inliningTarget, TypeError, ARGUMENTS_MUST_BE_ITERATORS);
242+
private static void checkIterator(Node node, Object obj) throws PException {
243+
if (!PyIterCheckNode.executeUncached(obj)) {
244+
throw PRaiseNode.raiseStatic(node, TypeError, ARGUMENTS_MUST_BE_ITERATORS);
257245
}
258246
}
259247
}

0 commit comments

Comments
 (0)