Skip to content

Commit e1d24b3

Browse files
msimacektimfel
authored andcommitted
Migrate usages of isCallable to a node
1 parent 9233d39 commit e1d24b3

File tree

11 files changed

+91
-58
lines changed

11 files changed

+91
-58
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsAcceptableBaseNode;
188188
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
189189
import com.oracle.graal.python.lib.CanBeDoubleNode;
190+
import com.oracle.graal.python.lib.PyCallableCheckNode;
190191
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
191192
import com.oracle.graal.python.lib.PyFloatFromString;
192193
import com.oracle.graal.python.lib.PyMappingCheckNode;
@@ -3016,8 +3017,8 @@ Object methodBuiltin(@SuppressWarnings("unused") Object cls, PBuiltinFunction fu
30163017

30173018
@Specialization
30183019
Object methodGeneric(@SuppressWarnings("unused") Object cls, Object func, Object self,
3019-
@CachedLibrary(limit = "3") PythonObjectLibrary dataModelLibrary) {
3020-
if (dataModelLibrary.isCallable(func)) {
3020+
@Cached PyCallableCheckNode callableCheck) {
3021+
if (callableCheck.execute(func)) {
30213022
return factory().createMethod(self, func);
30223023
} else {
30233024
throw raise(TypeError, ErrorMessages.FIRST_ARG_MUST_BE_CALLABLE);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
128128
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
129129
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
130+
import com.oracle.graal.python.lib.PyCallableCheckNode;
130131
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
131132
import com.oracle.graal.python.lib.PyNumberIndexNode;
132133
import com.oracle.graal.python.lib.PyObjectAsciiNode;
@@ -216,6 +217,7 @@
216217
import com.oracle.truffle.api.debug.Debugger;
217218
import com.oracle.truffle.api.dsl.Cached;
218219
import com.oracle.truffle.api.dsl.Cached.Shared;
220+
import com.oracle.truffle.api.dsl.Fallback;
219221
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
220222
import com.oracle.truffle.api.dsl.ImportStatic;
221223
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -1263,16 +1265,15 @@ static Object iter(VirtualFrame frame, Object object, @SuppressWarnings("unused"
12631265
return lib.getIteratorWithFrame(object, frame);
12641266
}
12651267

1266-
@Specialization(guards = {"lib.isCallable(callable)", "!isNoValue(sentinel)"}, limit = "1")
1268+
@Specialization(guards = {"callableCheck.execute(callable)", "!isNoValue(sentinel)"}, limit = "1")
12671269
Object iter(Object callable, Object sentinel,
1268-
@SuppressWarnings("unused") @CachedLibrary("callable") PythonObjectLibrary lib) {
1270+
@SuppressWarnings("unused") @Cached PyCallableCheckNode callableCheck) {
12691271
return factory().createSentinelIterator(callable, sentinel);
12701272
}
12711273

1272-
@Specialization(guards = {"!lib.isCallable(callable)", "!isNoValue(sentinel)"}, limit = "1")
1274+
@Fallback
12731275
@SuppressWarnings("unused")
1274-
Object iterNotCallable(Object callable, Object sentinel,
1275-
@CachedLibrary("callable") PythonObjectLibrary lib) {
1276+
Object iterNotCallable(Object callable, Object sentinel) {
12761277
throw raise(TypeError, ErrorMessages.ITER_V_MUST_BE_CALLABLE);
12771278
}
12781279
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,16 @@ private static String normalizeString(String encoding) {
911911
@Builtin(name = "register", minNumOfPositionalArgs = 1)
912912
@GenerateNodeFactory
913913
abstract static class RegisterNode extends PythonBuiltinNode {
914-
@Specialization(guards = "callableCheckNode.execute(frame, searchFunction)")
915-
Object lookup(@SuppressWarnings("unused") VirtualFrame frame, Object searchFunction,
914+
@Specialization(guards = "callableCheckNode.execute(searchFunction)")
915+
Object lookup(Object searchFunction,
916916
@SuppressWarnings("unused") @Cached PyCallableCheckNode callableCheckNode) {
917917
add(PythonContext.get(this), searchFunction);
918918
return null;
919919
}
920920

921921
@SuppressWarnings("unused")
922-
@Specialization(guards = "!callableCheckNode.execute(frame, searchFunction)")
923-
Object lookupNoCallble(VirtualFrame frame, Object searchFunction,
922+
@Specialization(guards = "!callableCheckNode.execute(searchFunction)")
923+
Object lookupNoCallble(Object searchFunction,
924924
@Cached PyCallableCheckNode callableCheckNode) {
925925
throw raise(TypeError, ARG_MUST_BE_CALLABLE);
926926
}
@@ -959,16 +959,16 @@ protected ArgumentClinicProvider getArgumentClinic() {
959959
return CodecsModuleBuiltinsClinicProviders.RegisterErrorNodeClinicProviderGen.INSTANCE;
960960
}
961961

962-
@Specialization(guards = "callableCheckNode.execute(frame, handler)")
963-
Object register(@SuppressWarnings("unused") VirtualFrame frame, String name, Object handler,
962+
@Specialization(guards = "callableCheckNode.execute(handler)")
963+
Object register(String name, Object handler,
964964
@SuppressWarnings("unused") @Cached PyCallableCheckNode callableCheckNode) {
965965
put(PythonContext.get(this), name, handler);
966966
return PNone.NONE;
967967
}
968968

969969
@SuppressWarnings("unused")
970-
@Specialization(guards = "!callableCheckNode.execute(frame, handler)")
971-
Object registerNoCallable(VirtualFrame frame, String name, Object handler,
970+
@Specialization(guards = "!callableCheckNode.execute(handler)")
971+
Object registerNoCallable(String name, Object handler,
972972
@Cached PyCallableCheckNode callableCheckNode) {
973973
throw raise(TypeError, HANDLER_MUST_BE_CALLABLE);
974974
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
import com.oracle.graal.python.builtins.PythonBuiltins;
6565
import com.oracle.graal.python.builtins.objects.PNone;
6666
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
67-
import com.oracle.graal.python.builtins.objects.code.PCode;
6867
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
68+
import com.oracle.graal.python.builtins.objects.code.PCode;
6969
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
7070
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
7171
import com.oracle.graal.python.builtins.objects.common.EmptyStorage;
@@ -83,9 +83,9 @@
8383
import com.oracle.graal.python.builtins.objects.method.PMethod;
8484
import com.oracle.graal.python.builtins.objects.module.PythonModule;
8585
import com.oracle.graal.python.builtins.objects.object.PythonObject;
86-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
8786
import com.oracle.graal.python.builtins.objects.set.PSet;
8887
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
88+
import com.oracle.graal.python.lib.PyObjectTypeCheck;
8989
import com.oracle.graal.python.nodes.ErrorMessages;
9090
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
9191
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import com.oracle.graal.python.builtins.objects.PNone;
5454
import com.oracle.graal.python.builtins.objects.ints.PInt;
5555
import com.oracle.graal.python.builtins.objects.module.PythonModule;
56-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
56+
import com.oracle.graal.python.lib.PyCallableCheckNode;
5757
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
5858
import com.oracle.graal.python.nodes.ErrorMessages;
5959
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
@@ -64,17 +64,20 @@
6464
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
6565
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
6666
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
67+
import com.oracle.graal.python.nodes.util.CannotCastException;
68+
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
6769
import com.oracle.graal.python.runtime.AsyncHandler;
70+
import com.oracle.graal.python.runtime.exception.PException;
6871
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6972
import com.oracle.graal.python.util.OverflowException;
7073
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7174
import com.oracle.truffle.api.dsl.Cached;
75+
import com.oracle.truffle.api.dsl.Cached.Shared;
7276
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7377
import com.oracle.truffle.api.dsl.NodeFactory;
7478
import com.oracle.truffle.api.dsl.Specialization;
7579
import com.oracle.truffle.api.dsl.TypeSystemReference;
7680
import com.oracle.truffle.api.frame.VirtualFrame;
77-
import com.oracle.truffle.api.library.CachedLibrary;
7881
import com.oracle.truffle.api.object.HiddenKey;
7982

8083
import sun.misc.Signal;
@@ -227,14 +230,20 @@ Object defaultIntHandler(@SuppressWarnings("unused") Object[] args) {
227230
@GenerateNodeFactory
228231
abstract static class SignalNode extends PythonTernaryBuiltinNode {
229232

230-
@Specialization(guards = "!idNumLib.isCallable(idNum)", limit = "1")
233+
@Specialization(guards = "!callableCheck.execute(idNum)", limit = "1")
231234
Object signalId(VirtualFrame frame, @SuppressWarnings("unused") PythonModule self, Object signal, Object idNum,
232-
@SuppressWarnings("unused") @CachedLibrary("idNum") PythonObjectLibrary idNumLib,
233-
@Cached PyNumberAsSizeNode asSizeNode) {
235+
@SuppressWarnings("unused") @Shared("callableCheck") @Cached PyCallableCheckNode callableCheck,
236+
@Shared("asSize") @Cached PyNumberAsSizeNode asSizeNode,
237+
@Cached CastToJavaIntExactNode cast) {
234238
// Note: CPython checks if id is the same reference as SIG_IGN/SIG_DFL constants, which
235239
// are instances of Handlers enum
236240
// The -1 fallback will be correctly reported as an error later on
237-
int id = idNum instanceof Integer ? (int) idNum : -1;
241+
int id;
242+
try {
243+
id = cast.execute(idNum);
244+
} catch (CannotCastException | PException e) {
245+
id = -1;
246+
}
238247
return signal(asSizeNode.executeExact(frame, signal), id);
239248
}
240249

@@ -255,10 +264,10 @@ private Object signal(int signum, int id) {
255264
return result;
256265
}
257266

258-
@Specialization(guards = "handlerLib.isCallable(handler)", limit = "1")
267+
@Specialization(guards = "callableCheck.execute(handler)", limit = "1")
259268
Object signalHandler(VirtualFrame frame, PythonModule self, Object signal, Object handler,
260-
@SuppressWarnings("unused") @CachedLibrary("handler") PythonObjectLibrary handlerLib,
261-
@Cached PyNumberAsSizeNode asSizeNode,
269+
@SuppressWarnings("unused") @Shared("callableCheck") @Cached PyCallableCheckNode callableCheck,
270+
@Shared("asSize") @Cached PyNumberAsSizeNode asSizeNode,
262271
@Cached ReadAttributeFromObjectNode readQueueNode,
263272
@Cached ReadAttributeFromObjectNode readSemaNode) {
264273
return signal(self, asSizeNode.executeExact(frame, signal), handler, readQueueNode, readSemaNode);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6363
import com.oracle.graal.python.builtins.objects.str.PString;
6464
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
65+
import com.oracle.graal.python.lib.PyCallableCheckNode;
6566
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
6667
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
6768
import com.oracle.graal.python.lib.PyObjectSizeNode;
@@ -600,7 +601,7 @@ private static void callShowWarning(PythonContext context, Object category, Obje
600601
return;
601602
}
602603

603-
if (!polib.isCallable(showFn)) {
604+
if (!PyCallableCheckNode.getUncached().execute(showFn)) {
604605
throw raise.raise(PythonBuiltinClassType.TypeError, "warnings._showwarnmsg() must be set to a callable");
605606
}
606607

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCFuncPtrBuiltins.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import com.oracle.graal.python.builtins.objects.str.PString;
107107
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
108108
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
109+
import com.oracle.graal.python.lib.PyCallableCheckNode;
109110
import com.oracle.graal.python.lib.PyLongCheckNode;
110111
import com.oracle.graal.python.nodes.PGuards;
111112
import com.oracle.graal.python.nodes.PNodeWithRaise;
@@ -196,10 +197,10 @@ Object usingNativePointer(Object type, Object[] args, @SuppressWarnings("unused"
196197
Object callback(Object type, Object[] args, @SuppressWarnings("unused") PKeyword[] kwds,
197198
@SuppressWarnings("unused") @Cached PyLongCheckNode longCheckNode,
198199
// @Cached KeepRefNode keepRefNode,
199-
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
200+
@Cached PyCallableCheckNode callableCheck,
200201
@Cached PyTypeStgDictNode pyTypeStgDictNode) {
201202
Object callable = args[0];
202-
if (!lib.isCallable(callable)) {
203+
if (!callableCheck.execute(callable)) {
203204
throw raise(TypeError, ARGUMENT_MUST_BE_CALLABLE_OR_INTEGER_FUNCTION_ADDRESS);
204205
}
205206

@@ -323,8 +324,8 @@ Object PyCFuncPtr_get_errcheck(PyCFuncPtrObject self, @SuppressWarnings("unused"
323324

324325
@Specialization(guards = "!isNoValue(value)")
325326
Object PyCFuncPtr_set_errcheck(PyCFuncPtrObject self, Object value,
326-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
327-
if (value != PNone.NONE && !lib.isCallable(value)) {
327+
@Cached PyCallableCheckNode callableCheck) {
328+
if (value != PNone.NONE && !callableCheck.execute(value)) {
328329
throw raise(TypeError, THE_ERRCHECK_ATTRIBUTE_MUST_BE_CALLABLE);
329330
}
330331
self.errcheck = value;
@@ -356,13 +357,13 @@ Object PyCFuncPtr_get_restype(PyCFuncPtrObject self, @SuppressWarnings("unused")
356357
Object PyCFuncPtr_set_restype(PyCFuncPtrObject self, Object value,
357358
@Cached("create(_check_retval_)") LookupAttributeInMRONode lookupAttr,
358359
@Cached PyTypeStgDictNode pyTypeStgDictNode,
359-
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
360+
@Cached PyCallableCheckNode callableCheck) {
360361
if (value == PNone.NONE) {
361362
self.checker = null;
362363
self.restype = null;
363364
return PNone.NONE;
364365
}
365-
if (pyTypeStgDictNode.execute(value) != null && !lib.isCallable(value)) {
366+
if (pyTypeStgDictNode.execute(value) != null && !callableCheck.execute(value)) {
366367
throw raise(TypeError, RESTYPE_MUST_BE_A_TYPE_A_CALLABLE_OR_NONE);
367368
}
368369
if (!PGuards.isPFunction(value)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCFuncPtrTypeBuiltins.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
import com.oracle.graal.python.builtins.objects.dict.PDict;
6666
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6767
import com.oracle.graal.python.builtins.objects.object.PythonObject;
68-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6968
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
69+
import com.oracle.graal.python.lib.PyCallableCheckNode;
7070
import com.oracle.graal.python.nodes.PGuards;
7171
import com.oracle.graal.python.nodes.PRaiseNode;
7272
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
@@ -116,7 +116,7 @@ Object PyCFuncPtrType_new(VirtualFrame frame, Object type, Object[] args, PKeywo
116116
@Cached GetInternalObjectArrayNode getArray,
117117
@Cached GetDictIfExistsNode getDict,
118118
@Cached SetDictNode setDict,
119-
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
119+
@Cached PyCallableCheckNode callableCheck,
120120
@CachedLibrary(limit = "1") HashingStorageLibrary hlib) {
121121
StgDictObject stgdict = factory().createStgDictObject(PythonBuiltinClassType.StgDict);
122122

@@ -167,7 +167,7 @@ Object PyCFuncPtrType_new(VirtualFrame frame, Object type, Object[] args, PKeywo
167167
ob = hlib.getItem(stgdict.getDictStorage(), _restype_);
168168
if (!PGuards.isPNone(ob)) {
169169
StgDictObject dict = pyTypeStgDictNode.execute(ob);
170-
if (dict == null && !lib.isCallable(ob)) {
170+
if (dict == null && !callableCheck.execute(ob)) {
171171
throw raise(TypeError, RESTYPE_MUST_BE_A_TYPE_A_CALLABLE_OR_NONE1);
172172
}
173173
stgdict.restype = ob;
@@ -211,9 +211,9 @@ static Object[] converters_from_argtypes(Object[] args,
211211
* Although specific examples reported relate specifically to unions and
212212
* not bitfields, the bitfields check is also being disabled as a
213213
* precaution.
214-
214+
215215
StgDictObject *stgdict = PyType_stgdict(tp);
216-
216+
217217
if (stgdict != NULL) {
218218
if (stgdict.flags & TYPEFLAG_HASUNION) {
219219
Py_DECREF(converters);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot;
9696
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
9797
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
98+
import com.oracle.graal.python.lib.PyCallableCheckNode;
9899
import com.oracle.graal.python.lib.PyMappingCheckNode;
99100
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
100101
import com.oracle.graal.python.lib.PyObjectLookupAttr;
@@ -510,8 +511,8 @@ public Object invokeMember(String member, Object[] arguments,
510511

511512
@ExportMessage
512513
public boolean isExecutable(
513-
@CachedLibrary("this") PythonObjectLibrary dataModelLibrary) {
514-
return dataModelLibrary.isCallable(this);
514+
@Cached PyCallableCheckNode callableCheck) {
515+
return callableCheck.execute(this);
515516
}
516517

517518
@ExportMessage
@@ -1176,7 +1177,7 @@ public abstract static class PKeyInfoNode extends Node {
11761177
static boolean access(Object object, String attrKeyName, int type,
11771178
@Cached("createForceType()") ReadAttributeFromObjectNode readTypeAttrNode,
11781179
@Cached ReadAttributeFromObjectNode readObjectAttrNode,
1179-
@CachedLibrary(limit = "2") PythonObjectLibrary dataModelLibrary,
1180+
@Cached PyCallableCheckNode callableCheck,
11801181
@Cached LookupInheritedAttributeNode.Dynamic getGetNode,
11811182
@Cached LookupInheritedAttributeNode.Dynamic getSetNode,
11821183
@Cached LookupInheritedAttributeNode.Dynamic getDeleteNode,
@@ -1237,7 +1238,7 @@ static boolean access(Object object, String attrKeyName, int type,
12371238
return false;
12381239
}
12391240
}
1240-
return dataModelLibrary.isCallable(attr);
1241+
return callableCheck.execute(attr);
12411242
}
12421243
return false;
12431244
case READ_SIDE_EFFECTS:
@@ -1339,12 +1340,12 @@ Object doVarargsBuiltinMethod(Object receiver, Object[] arguments,
13391340
return callVarargsMethodNode.execute(null, receiver, convertedArgs, PKeyword.EMPTY_KEYWORDS);
13401341
}
13411342

1342-
@Specialization(limit = "1", replaces = "doVarargsBuiltinMethod")
1343+
@Specialization(replaces = "doVarargsBuiltinMethod")
13431344
Object doExecute(Object receiver, Object[] arguments,
1344-
@CachedLibrary("receiver") PythonObjectLibrary dataModelLibrary,
1345+
@Cached PyCallableCheckNode callableCheck,
13451346
@Exclusive @Cached CallNode callNode,
13461347
@Exclusive @Cached ArgumentsFromForeignNode convertArgsNode) throws UnsupportedMessageException {
1347-
if (!dataModelLibrary.isCallable(receiver)) {
1348+
if (!callableCheck.execute(receiver)) {
13481349
throw UnsupportedMessageException.create();
13491350
}
13501351
Object[] convertedArgs = convertArgsNode.execute(arguments);

0 commit comments

Comments
 (0)