136136import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
137137import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
138138import com .oracle .graal .python .nodes .function .builtins .PythonBinaryClinicBuiltinNode ;
139+ import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
139140import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
140141import com .oracle .graal .python .nodes .function .builtins .PythonUnaryClinicBuiltinNode ;
141142import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
148149import com .oracle .graal .python .util .PythonUtils ;
149150import com .oracle .truffle .api .CompilerAsserts ;
150151import com .oracle .truffle .api .CompilerDirectives ;
152+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
151153import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
152154import com .oracle .truffle .api .TruffleLogger ;
153155import com .oracle .truffle .api .dsl .Bind ;
@@ -182,6 +184,9 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
182184 return CtypesModuleBuiltinsFactory .getFactories ();
183185 }
184186
187+ @ CompilationFinal private Object strlenFunction ;
188+ @ CompilationFinal private Object memcpyFunction ;
189+
185190 private static final String NFI_LANGUAGE = "nfi" ;
186191
187192 protected static final int FUNCFLAG_STDCALL = 0x0 ;
@@ -224,17 +229,24 @@ public void postInitialize(Python3Core core) {
224229 ctypesModule .setAttribute ("RTLD_GLOBAL" , RTLD_GLOBAL .getValueIfDefined ());
225230
226231 DLHandler handle = DlOpenNode .loadNFILibrary (core .getContext (), NFIBackend .NATIVE , "" , rtldLocal );
227- setCtypeNFIHelpers (core .getContext (), handle );
232+ setCtypeNFIHelpers (this , core .getContext (), handle );
228233 NativeFunction memmove = MemMoveFunction .create (handle , core .getContext ());
229234 ctypesModule .setAttribute ("_memmove_addr" , factory .createNativeVoidPtr (memmove , memmove .adr ));
230235 NativeFunction memset = MemSetFunction .create (handle , core .getContext ());
231236 ctypesModule .setAttribute ("_memset_addr" , factory .createNativeVoidPtr (memset , memset .adr ));
232237 }
233238
234- private static void setCtypeNFIHelpers (PythonContext context , DLHandler h ) {
235- Object strlen = createNFIHelperFunction (context , h , "strlen" , "(POINTER):UINT32" );
236- Object memcpy = createNFIHelperFunction (context , h , "memcpy" , "([UINT8], POINTER, UINT32):POINTER" );
237- context .setCtypesNFIHelpers (strlen , memcpy );
239+ Object getStrlenFunction () {
240+ return strlenFunction ;
241+ }
242+
243+ Object getMemcpyFunction () {
244+ return memcpyFunction ;
245+ }
246+
247+ private static void setCtypeNFIHelpers (CtypesModuleBuiltins ctypesModuleBuiltins , PythonContext context , DLHandler h ) {
248+ ctypesModuleBuiltins .strlenFunction = createNFIHelperFunction (context , h , "strlen" , "(POINTER):UINT32" );
249+ ctypesModuleBuiltins .memcpyFunction = createNFIHelperFunction (context , h , "memcpy" , "([UINT8], POINTER, UINT32):POINTER" );
238250 }
239251
240252 private static Object createNFIHelperFunction (PythonContext context , DLHandler h , String name , String signature ) {
@@ -879,12 +891,12 @@ protected static Object getObjectAt(PythonContext context, Object ptr) {
879891 return ptr ;
880892 }
881893
882- @ Builtin (name = "call_function" , minNumOfPositionalArgs = 1 )
894+ @ Builtin (name = "call_function" , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
883895 @ GenerateNodeFactory
884- protected abstract static class CallFunctionNode extends PythonBinaryBuiltinNode {
896+ protected abstract static class CallFunctionNode extends PythonTernaryBuiltinNode {
885897
886898 @ Specialization
887- Object call_function (VirtualFrame frame , Object f , PTuple arguments ,
899+ Object call_function (VirtualFrame frame , PythonModule ctypesModule , Object f , PTuple arguments ,
888900 @ Cached AuditNode auditNode ,
889901 @ Cached CallProcNode callProcNode ,
890902 @ Cached GetInternalObjectArrayNode getArray ,
@@ -902,7 +914,8 @@ Object call_function(VirtualFrame frame, Object f, PTuple arguments,
902914 null ,
903915 ctypes ,
904916 factory (),
905- getContext ());
917+ getContext (),
918+ (CtypesModuleBuiltins ) ctypesModule .getBuiltins ());
906919 }
907920 }
908921
@@ -928,12 +941,12 @@ Object error(VirtualFrame frame, Object o) {
928941 }
929942 }
930943
931- @ Builtin (name = "call_cdeclfunction" , minNumOfPositionalArgs = 1 )
944+ @ Builtin (name = "call_cdeclfunction" , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
932945 @ GenerateNodeFactory
933- protected abstract static class CallCdeclfunctionNode extends PythonBinaryBuiltinNode {
946+ protected abstract static class CallCdeclfunctionNode extends PythonTernaryBuiltinNode {
934947
935948 @ Specialization
936- Object doit (VirtualFrame frame , Object f , PTuple arguments ,
949+ Object doit (VirtualFrame frame , PythonModule ctypesModule , Object f , PTuple arguments ,
937950 @ Cached AuditNode auditNode ,
938951 @ Cached GetInternalObjectArrayNode getArray ,
939952 @ Cached CallProcNode callProcNode ,
@@ -951,7 +964,8 @@ Object doit(VirtualFrame frame, Object f, PTuple arguments,
951964 null ,
952965 ctypes ,
953966 factory (),
954- getContext ());
967+ getContext (),
968+ (CtypesModuleBuiltins ) ctypesModule .getBuiltins ());
955969 }
956970 }
957971
@@ -976,7 +990,8 @@ protected abstract static class CallProcNode extends PNodeWithRaise {
976990 abstract Object execute (VirtualFrame frame , NativeFunction pProc , Object [] argtuple , int flags , Object [] argtypes , Object [] converters , Object restype , Object checker ,
977991 CtypesThreadState state ,
978992 PythonObjectFactory factory ,
979- PythonContext context );
993+ PythonContext context ,
994+ CtypesModuleBuiltins ctypesModuleBuiltins );
980995
981996 /*
982997 * bpo-13097: Max number of arguments _ctypes_callproc will accept.
@@ -997,6 +1012,7 @@ Object _ctypes_callproc(VirtualFrame frame,
9971012 @ SuppressWarnings ("unused" ) CtypesThreadState state ,
9981013 PythonObjectFactory factory ,
9991014 PythonContext context ,
1015+ CtypesModuleBuiltins ctypesModuleBuiltins ,
10001016 @ Cached ConvParamNode convParamNode ,
10011017 @ Cached PyTypeStgDictNode pyTypeStgDictNode ,
10021018 @ Cached CallNode callNode ,
@@ -1078,7 +1094,7 @@ Object _ctypes_callproc(VirtualFrame frame,
10781094 throw CompilerDirectives .shouldNotReachHere (e );
10791095 }
10801096 } else if (!isLLVM && ilib .isPointer (result )) {
1081- result = getNativeBytes (getContext (), result , getRaiseNode ());
1097+ result = getNativeBytes (ctypesModuleBuiltins , getContext (), result , getRaiseNode ());
10821098 } else if (ilib .isNumber (result )) {
10831099 byte [] bytes = new byte [rtype .size ];
10841100 CtypesNodes .setValue (rtype .type , bytes , 0 , result );
@@ -1139,6 +1155,7 @@ Object doManaged(NativeFunction pProc,
11391155 @ SuppressWarnings ("unused" ) CtypesThreadState state ,
11401156 @ SuppressWarnings ("unused" ) PythonObjectFactory factory ,
11411157 @ SuppressWarnings ("unused" ) PythonContext context ,
1158+ @ SuppressWarnings ("unused" ) CtypesModuleBuiltins ctypesModuleBuiltins ,
11421159 @ CachedLibrary (limit = "1" ) InteropLibrary ilib ) {
11431160 return callManagedFunction (pProc , argarray , ilib );
11441161 }
@@ -1161,11 +1178,11 @@ protected static Object getFunction(NativeFunction pProc, String signature, Pyth
11611178 }
11621179
11631180 @ TruffleBoundary
1164- private static byte [] getNativeBytes (PythonContext context , Object pointer , PRaiseNode raiseNode ) {
1181+ private static byte [] getNativeBytes (CtypesModuleBuiltins ctypesModuleBuiltins , PythonContext context , Object pointer , PRaiseNode raiseNode ) {
11651182 try {
1166- long size = (Long ) InteropLibrary .getUncached ().execute (context .getStrlenFunction (), pointer );
1183+ long size = (Long ) InteropLibrary .getUncached ().execute (ctypesModuleBuiltins .getStrlenFunction (), pointer );
11671184 byte [] bytes = new byte [(int ) size ];
1168- InteropLibrary .getUncached ().execute (context .getMemcpyFunction (), context .getEnv ().asGuestValue (bytes ), pointer , size );
1185+ InteropLibrary .getUncached ().execute (ctypesModuleBuiltins .getMemcpyFunction (), context .getEnv ().asGuestValue (bytes ), pointer , size );
11691186 return bytes ;
11701187 } catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e ) {
11711188 throw raiseNode .raise (SystemError , e );
0 commit comments