Bug description:
ctypes.util.wrap_dll_function builds argtypes from every annotated parameter, including keyword-only, *args and **kwargs ones. argtypes describes positional arguments only, so the generated function pointer asks for an argument that the C function does not have.
import ctypes
from ctypes import c_char_p, c_int, c_size_t
from ctypes.util import wrap_dll_function
libc = ctypes.CDLL("libc.so.6")
@wrap_dll_function(libc)
def strlen(s: c_char_p, *, extra: c_int) -> c_size_t:
pass
print(strlen.argtypes)
print(strlen(b"abcd"))
(<class 'ctypes.c_char_p'>, <class 'ctypes.c_int'>)
Traceback (most recent call last):
File "issue-repro.py", line 12, in <module>
print(strlen(b"abcd"))
~~~~~~^^^^^^^^^
TypeError: this function takes at least 2 arguments (1 given)
Passing the annotated parameter as a keyword fails the same way; only strlen(b"abcd", 1) reaches the C function, and the stub's own signature forbids that. An annotated *args or **kwargs parameter produces the same extra entry.
Expected: a parameter that has no positional counterpart contributes no argtypes entry, so strlen.argtypes is (c_char_p,) and strlen(b"abcd") returns 4. An error raised at decoration time would resolve it too.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
Bug description:
ctypes.util.wrap_dll_functionbuildsargtypesfrom every annotated parameter, including keyword-only,*argsand**kwargsones.argtypesdescribes positional arguments only, so the generated function pointer asks for an argument that the C function does not have.Passing the annotated parameter as a keyword fails the same way; only
strlen(b"abcd", 1)reaches the C function, and the stub's own signature forbids that. An annotated*argsor**kwargsparameter produces the same extra entry.Expected: a parameter that has no positional counterpart contributes no
argtypesentry, sostrlen.argtypesis(c_char_p,)andstrlen(b"abcd")returns 4. An error raised at decoration time would resolve it too.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs