The idea is to add a decorator that extracts type annotations and a name for a function signature, and then use that information to generate argtypes and restype for a _CFuncPtr object. For example:
It would just be a nice-to-have if things like help() in the REPL (or ? in ipython) reproduced the docstring if one was provided on the stub. I think this could also be used by tools like sphinx. Whenever I write a decorator I tend to use functools.wraps, but I think the only relevant thing here is actually the .__doc__ member.
Activity
gh-153903: Add a `ctypes` decorator for generating function pointers …
pythongh-153903: Fix ctypes wrap_dll_function() with string annotations
gh-153903: Fix ctypes wrap_dll_function() with string annotations (#1…
gh-153903: Use `@ctypes.util.wrap_dll_function()` with pythonapi (#15…
gh-153903: Use `@ctypes.util.wrap_dll_function()` (#154122)
WardBrian commented on Jul 21, 2026
It would also be nice if this propagated doc comments, which I don't believe gh-153904 does
ZeroIntensity commented on Jul 21, 2026
Do you have a compelling use case for that?
WardBrian commented on Jul 21, 2026
It would just be a nice-to-have if things like help() in the REPL (or ? in ipython) reproduced the docstring if one was provided on the stub. I think this could also be used by tools like sphinx. Whenever I write a decorator I tend to use functools.wraps, but I think the only relevant thing here is actually the .__doc__ member.
Currently, if you use the example in the docs:
help(PyObject_GetAttrString) would print the help for ctypes._FuncPtr. It would be nice if a user could write something like
And have that string not just be swallowed/ignored
ZeroIntensity commented on Jul 21, 2026
Seems reasonable. Would you like to submit a PR?
WardBrian commented on Jul 21, 2026
Happy to!
gh-153903: Copy docstrings and other metadata in `ctypes.util.wrap_dl…
pythongh-153903: Copy docstrings and other metadata in `ctypes.util.w…