Skip to content

Commit d6fd4e7

Browse files
committed
Fix receiving args in callbacks
1 parent 642d18f commit d6fd4e7

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ protected ArgumentClinicProvider getArgumentClinic() {
267267

268268
@Specialization(limit = "3")
269269
Object CDataType_from_buffer_copy(Object type, Object buffer, int offset,
270+
@Bind("this") Node inliningTarget,
270271
@CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib,
272+
@Cached PointerNodes.WriteBytesNode writeBytesNode,
271273
@Cached AuditNode auditNode,
272274
@Cached PyTypeStgDictNode pyTypeStgDictNode) {
273275
try {
@@ -288,10 +290,9 @@ Object CDataType_from_buffer_copy(Object type, Object buffer, int offset,
288290

289291
CDataObject result = factory().createCDataObject(type);
290292
GenericPyCDataNew(dict, result);
291-
// memcpy(result.b_ptr, buffer.buf + offset, dict.size);
292293
byte[] slice = new byte[dict.size];
293294
bufferLib.readIntoByteArray(buffer, offset, slice, 0, dict.size);
294-
result.b_ptr = Pointer.bytes(slice);
295+
writeBytesNode.execute(inliningTarget, result.b_ptr, slice);
295296
return result;
296297
} finally {
297298
bufferLib.release(buffer);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oracle.graal.python.builtins.modules.ctypes.FFIType.FieldSet;
5858
import com.oracle.graal.python.builtins.modules.ctypes.StgDictBuiltins.PyTypeStgDictNode;
5959
import com.oracle.graal.python.builtins.modules.ctypes.memory.Pointer;
60+
import com.oracle.graal.python.builtins.modules.ctypes.memory.PointerNodes;
6061
import com.oracle.graal.python.builtins.objects.PNone;
6162
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6263
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
@@ -139,6 +140,7 @@ Object execute(Object[] pArgs,
139140
@Cached CallNode callNode,
140141
@Cached WriteUnraisableNode writeUnraisableNode,
141142
@Cached WarnNode warnNode,
143+
@Cached PointerNodes.MemcpyNode memcpyNode,
142144
@Cached PRaiseNode raiseNode) throws ArityException {
143145
Object[] converters = thunk.converters;
144146
int nArgs = converters.length;
@@ -150,7 +152,6 @@ Object execute(Object[] pArgs,
150152
try {
151153
Object[] arglist = new Object[nArgs];
152154
for (int i = 0; i < nArgs; ++i) {
153-
StgDictObject dict = pyTypeStgDictNode.execute(converters[i]);
154155
Object arg = pArgs[i];
155156
if (lib.isPointer(arg)) {
156157
try {
@@ -159,15 +160,16 @@ Object execute(Object[] pArgs,
159160
throw CompilerDirectives.shouldNotReachHere(e);
160161
}
161162
}
163+
FFIType argType = thunk.atypes[i];
164+
Pointer value = Pointer.create(argType, argType.size, arg, 0);
165+
StgDictObject dict = pyTypeStgDictNode.execute(converters[i]);
162166
if (dict != null &&
163167
dict.getfunc != FieldGet.nil &&
164168
!pyTypeCheck.ctypesSimpleInstance(inliningTarget, converters[i], getBaseClassNode, isSameTypeNode)) {
165-
FFIType type = dict.ffi_type_pointer;
166-
Pointer ptr = Pointer.create(type, type.size, arg, 0);
167-
arglist[i] = getFuncNode.execute(dict.getfunc, ptr, dict.size);
169+
arglist[i] = getFuncNode.execute(dict.getfunc, value, dict.size);
168170
} else if (dict != null) {
169171
CDataObject obj = (CDataObject) callNode.execute(converters[i]);
170-
obj.b_ptr = Pointer.nativeMemory(arg).createReference();
172+
memcpyNode.execute(inliningTarget, obj.b_ptr, value, dict.size);
171173
arglist[i] = obj;
172174
} else {
173175
throw raiseNode.raise(TypeError, CANNOT_BUILD_PARAMETER);

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,21 @@ Object usingNativePointer(Object type, Object[] args, @SuppressWarnings("unused"
188188
@Bind("this") Node inliningTarget,
189189
@SuppressWarnings("unused") @Cached PyLongCheckNode longCheckNode,
190190
@Cached PointerNodes.PointerFromLongNode pointerFromLongNode,
191+
@Cached PointerNodes.WritePointerNode writePointerNode,
191192
@Cached PyTypeStgDictNode pyTypeStgDictNode) {
192193
CDataObject ob = factory().createPyCFuncPtrObject(type);
193194
StgDictObject dict = pyTypeStgDictNode.checkAbstractClass(type, getRaiseNode());
194195
GenericPyCDataNew(dict, ob);
195-
ob.b_ptr = pointerFromLongNode.execute(inliningTarget, args[0]).createReference();
196+
Pointer value = pointerFromLongNode.execute(inliningTarget, args[0]);
197+
writePointerNode.execute(inliningTarget, ob.b_ptr, value);
196198
return ob;
197199
}
198200

199201
@Specialization(guards = {"args.length > 0", "!isPTuple(args)", "!isLong(args, longCheckNode)"})
200202
Object callback(VirtualFrame frame, Object type, Object[] args, @SuppressWarnings("unused") PKeyword[] kwds,
203+
@Bind("this") Node inliningTarget,
201204
@SuppressWarnings("unused") @Cached PyLongCheckNode longCheckNode,
205+
@Cached PointerNodes.WritePointerNode writePointerNode,
202206
@Cached KeepRefNode keepRefNode,
203207
@Cached PyCallableCheckNode callableCheck,
204208
@Cached PyTypeStgDictNode pyTypeStgDictNode) {
@@ -207,20 +211,7 @@ Object callback(VirtualFrame frame, Object type, Object[] args, @SuppressWarning
207211
throw raise(TypeError, ARGUMENT_MUST_BE_CALLABLE_OR_INTEGER_FUNCTION_ADDRESS);
208212
}
209213

210-
/*
211-
* XXX XXX This would allow passing additional options. For COM method
212-
* *implementations*, we would probably want different behaviour than in 'normal'
213-
* callback functions: return a HRESULT if an exception occurs in the callback, and
214-
* print the traceback not only on the console, but also to OutputDebugString() or
215-
* something like that.
216-
*/
217-
/*
218-
* if (kwds && _PyDict_GetItemIdWithError(kwds, &PyId_options)) { ... } else if
219-
* (PyErr_Occurred()) { return NULL; }
220-
*/
221-
222214
StgDictObject dict = pyTypeStgDictNode.checkAbstractClass(type, getRaiseNode());
223-
/* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */
224215
if (dict == null || dict.argtypes == null) {
225216
throw raise(TypeError, CANNOT_CONSTRUCT_INSTANCE_OF_THIS_CLASS_NO_ARGTYPES);
226217
}
@@ -230,8 +221,7 @@ Object callback(VirtualFrame frame, Object type, Object[] args, @SuppressWarning
230221
GenericPyCDataNew(dict, self);
231222
self.callable = callable;
232223
self.thunk = thunk;
233-
self.b_ptr = Pointer.nativeMemory(thunk.pcl_exec).createReference();
234-
224+
writePointerNode.execute(inliningTarget, self.b_ptr, Pointer.nativeMemory(thunk.pcl_exec));
235225
keepRefNode.execute(frame, self, 0, thunk);
236226
return self;
237227
}
@@ -774,6 +764,7 @@ Object PyCFuncPtr_FromDll(VirtualFrame frame, Object type, Object[] args,
774764
@Cached PyTypeCheck pyTypeCheck,
775765
@Cached CtypesDlSymNode dlSymNode,
776766
@Cached PointerNodes.PointerFromLongNode pointerFromLongNode,
767+
@Cached PointerNodes.WritePointerNode writePointerNode,
777768
@Cached PyLongCheckNode longCheckNode,
778769
@Cached GetInternalObjectArrayNode getArray,
779770
@Cached CastToTruffleStringNode toString,
@@ -815,7 +806,7 @@ Object PyCFuncPtr_FromDll(VirtualFrame frame, Object type, Object[] args,
815806
self.paramflags = paramflags;
816807

817808
Object addressObj = address instanceof PythonNativeVoidPtr ptr ? ptr.getPointerObject() : address;
818-
self.b_ptr = Pointer.nativeMemory(addressObj).createReference();
809+
writePointerNode.execute(inliningTarget, self.b_ptr, Pointer.nativeMemory(addressObj));
819810
keepRefNode.execute(frame, self, 0, dll);
820811

821812
self.callable = self;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ protected Object StructUnionTypeNew(VirtualFrame frame, Object type, Object[] ar
163163
if (resDict == null) {
164164
resDict = factory().createDictFixedStorage((PythonObject) result);
165165
}
166-
/* keep this for bw compatibility */
167166
if (getItemResDict.hasKey(resDict.getDictStorage(), T__ABSTRACT_)) {
168167
return result;
169168
}
@@ -177,7 +176,7 @@ protected Object StructUnionTypeNew(VirtualFrame frame, Object type, Object[] ar
177176
* requirements of the instances
178177
*/
179178
dict.setDictStorage(addAllToOtherNode.execute(frame, resDict.getDictStorage(), dict.getDictStorage()));
180-
setDict.execute((PythonObject) result, dict);
179+
setDict.execute(result, dict);
181180
dict.format = T_UPPER_B;
182181

183182
dict.paramfunc = CArgObjectBuiltins.StructUnionTypeParamFunc;

0 commit comments

Comments
 (0)