You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On wasm targets, test_code fails. It's because of a bad function pointer cast. The problematic code is as follows:
importctypespy=ctypes.pythonapifreefunc=ctypes.CFUNCTYPE(None, ctypes.c_voidp)
RequestCodeExtraIndex=py._PyEval_RequestCodeExtraIndexRequestCodeExtraIndex.argtypes= (freefunc,)
RequestCodeExtraIndex.restype=ctypes.c_ssize_tSetExtra=py._PyCode_SetExtraSetExtra.argtypes= (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
SetExtra.restype=ctypes.c_intLAST_FREED=Nonedefmyfree(ptr):
globalLAST_FREEDLAST_FREED=ptrFREE_FUNC=freefunc(myfree)
FREE_INDEX=RequestCodeExtraIndex(FREE_FUNC)
# Verify that the provided free function gets invoked# when the code object is cleaned up.f=eval('lambda:42')
SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(100))
delf# crashes!!
The problem: freefunc = ctypes.CFUNCTYPE(None, ctypes.c_voidp) declares a function with signature int f(void*). However, the definition of freefunc is void f(void*). These function signatures do not agree, and trying to make the call crashes.
Due to a bug(?) ctypes can produce closures that have void return type but it can't produce simple functions with void return type.
Activity
hoodmane commentedon Apr 2, 2022
On wasm targets,
test_codefails. It's because of a bad function pointer cast. The problematic code is as follows:The problem:
freefunc = ctypes.CFUNCTYPE(None, ctypes.c_voidp)declares a function with signatureint f(void*). However, the definition offreefuncisvoid f(void*). These function signatures do not agree, and trying to make the call crashes.Due to a bug(?) ctypes can produce closures that have
voidreturn type but it can't produce simple functions with void return type.Closures code: checks if
restype == Py_Noneand if so gives the callback a void return typehttps://github.com/python/cpython/blob/main/Modules/_ctypes/callbacks.c#L381
Direct call code: calls
_ctypes_get_ffi_typewhich never returnsffi_type_void:cpython/Modules/_ctypes/callproc.c
Line 1212 in b183f48
gh-91353: Fix void return type handling in ctypes (GH-32246)
corona10 commentedon Jul 12, 2022
I think that we can close this issue. Please feel free to reopen this issue if needed.
tiran commentedon Jul 12, 2022
Yeah, the issue is fixed in 3.11 and main branches.