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.

classification
Title: Ctypes._FuncPtr.restype doesn't handle NoneType correctly
Type: behavior Stage:
Components: ctypes Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, thijsmiedema94
Priority: normal Keywords:

Created on 2021-02-12 14:44 by thijsmiedema94, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
minimal_example_ctypes_nonetype_behaviour.py thijsmiedema94, 2021-02-12 14:44 ctypes _FuncPtr.restype behaviour for None/NoneType
Messages (2)
msg386867 - (view) Author: Thijs Miedema (thijsmiedema94) * Date: 2021-02-12 14:44
When setting the restype of a ctypes._FuncPtr you can use None to indicate a void function. However, I use inspect.signature to dynamically bind python functions to a DLL, and that doesn't return None but NoneType if the type hint return type is None starting at python 3.10.

This change caused me to set the restype to NoneType, which makes ctypes cause a cryptic "TypeError: NoneType takes no arguments" upon returning from a successfully executed C function. 

The included example demonstrates the differing results from inspect, a working example, a failing example and an example that works in 3.9 but fails in 3.10.

Proposed solution: Treat NoneType as None for ctypes._FuncPtr.restype
msg386887 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-02-12 23:03
NoneType would have to be replaced with None in make_funcptrtype_dict() and PyCFuncPtr_set_restype() in Modules/_ctypes/_ctypes.c. In the C API, NoneType is (PyObject *)&_PyNone_Type.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87374
2021-02-12 23:03:12eryksunsetnosy: + eryksun
messages: + msg386887
2021-02-12 14:44:57thijsmiedema94create