Text of bugs.python.org › issue47197 as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout.

Open the live page

Issue 47197: ctypes mishandles `void` return type - Python tracker

Python Home About News Documentation Downloads Community Foundation Developer's Guide Issue Tracker Issues Search Random Issue Summaries Issues with patch Easy issues Stats User Administration User List Committer List Help Tracker Documentation Tracker Development Report Tracker Problem Issue47197 ➜ This issue tracker has been migrated to GitHub , and is currently read-only . For more information, see the GitHub FAQs in the Python's Developer Guide.

Created on 2022-04-02 01:14 by hoodchatham , last changed 2022-04-11 14:59 by admin . Pull Requests URL Status Linked Edit PR 32246 open hoodmane , 2022-04-02 01:59 Messages (1) msg416526 - (view) Author: Hood Chatham (hoodchatham) * Date: 2022-04-02 01:14 On wasm targets, `test_code` fails. It's because of a bad function pointer cast. The problematic code is as follows: ```py import ctypes py = ctypes.pythonapi freefunc = ctypes.CFUNCTYPE(None, ctypes.c_voidp) RequestCodeExtraIndex = py._PyEval_RequestCodeExtraIndex RequestCodeExtraIndex.argtypes = (freefunc,) RequestCodeExtraIndex.restype = ctypes.c_ssize_t SetExtra = py._PyCode_SetExtra SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp) SetExtra.restype = ctypes.c_int LAST_FREED = None def myfree(ptr): global LAST_FREED LAST_FREED = ptr FREE_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)) del f # 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. Closures code: checks if `restype == Py_None` and if so gives the callback a void return type https://github.com/python/cpython/blob/main/Modules/_ctypes/callbacks.c#L381 Direct call code: calls `_ctypes_get_ffi_type` which never returns `ffi_type_void`: https://github.com/python/cpython/blob/b183f486493e7e4c332566392ef18c6b346a6561/Modules/_ctypes/callproc.c#L1212 History Date User Action Args 2022-04-11 14:59:58 admin set github: 91353 2022-04-03 21:02:00 christian.heimes set type: behavior components: + Tests versions: + Python 3.11 2022-04-03 21:00:45 christian.heimes link issue40280 dependencies 2022-04-03 16:05:10 hoodchatham set nosy: + amaury.forgeotdarc , belopolsky , meador.inge 2022-04-02 01:59:45 hoodmane set keywords: + patch nosy: + hoodmane pull_requests: + pull_request30317 stage: patch review 2022-04-02 01:14:09 hoodchatham create Supported by The Python Software Foundation , Powered by Roundup Copyright © 1990-2022, Python Software Foundation Legal Statements