Skip to content

wrap_dll_function builds argtypes from non-positional parameters #156500

Description

@fedonman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixestopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions