Skip to content

Annotation-based decorator for ctypes function pointers #153903

Closed

Description

@ZeroIntensity

Feature or enhancement

Proposal:

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:

@wrap_dll_function(ctypes.pythonapi)
def PyObject_GetAttrString(op: ctypes.py_object, attr: ctypes.c_char_p) -> ctypes.py_object:
    ...

See #104533 for prior work.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/annotation-based-sugar-for-ctypes/26579

Linked PRs

Activity

  1. added a commit that references this issue on Jul 18, 2026
  2. moved this from Todo to Done in Sprinton Jul 18, 2026
  3. added 4 commits that reference this issue on Jul 19, 2026
  4. WardBrian commented on Jul 21, 2026

    @WardBrian
    Contributor

    It would also be nice if this propagated doc comments, which I don't believe gh-153904 does

  5. ZeroIntensity commented on Jul 21, 2026

    @ZeroIntensity
    MemberAuthor

    Do you have a compelling use case for that?

  6. WardBrian commented on Jul 21, 2026

    @WardBrian
    Contributor

    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:

    import ctypes
    from ctypes.util import wrap_dll_function
    
    @wrap_dll_function(ctypes.pythonapi)
    def PyObject_GetAttrString(op: ctypes.py_object, attr: ctypes.c_char_p) -> ctypes.py_object:
       pass

    help(PyObject_GetAttrString) would print the help for ctypes._FuncPtr. It would be nice if a user could write something like

    @wrap_dll_function(ctypes.pythonapi)
    def PyObject_GetAttrString(op: ctypes.py_object, attr: ctypes.c_char_p) -> ctypes.py_object:
       """Calls the internal ..."""
       pass
    

    And have that string not just be swallowed/ignored

  7. ZeroIntensity commented on Jul 21, 2026

    @ZeroIntensity
    MemberAuthor

    Seems reasonable. Would you like to submit a PR?

  8. WardBrian commented on Jul 21, 2026

    @WardBrian
    Contributor

    Happy to!

  9. added a commit that references this issue on Jul 23, 2026
  10. added a commit that references this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

sprintstdlibStandard Library Python modules in the Lib/ directorytopic-ctypestype-featureA feature request or enhancement

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions