Skip to content

gh-153903: Fix ctypes wrap_dll_function() with string annotations#154042

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:main
Jul 19, 2026
Merged

gh-153903: Fix ctypes wrap_dll_function() with string annotations#154042
vstinner merged 1 commit into
python:mainfrom
vstinner:main

Conversation

@vstinner

@vstinner vstinner commented Jul 19, 2026

Copy link
Copy Markdown
Member

Call get_annotations() with eval_str=True.

@vstinner

Copy link
Copy Markdown
Member Author

@ctypes.util.struct has the same bug: see PR gh-154040 which fixes it.

@vstinner

Copy link
Copy Markdown
Member Author

cc @ZeroIntensity

@vstinner

Copy link
Copy Markdown
Member Author

With this change, the following example:

import ctypes.util

@ctypes.util.wrap_dll_function(ctypes.pythonapi)
def Py_GetVersion() -> 'invalid return type':
    ...

now fails with SyntaxError:

Traceback (most recent call last):
  File "/home/vstinner/python/main/ann.py", line 3, in <module>
    @ctypes.util.wrap_dll_function(ctypes.pythonapi)
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/home/vstinner/python/main/Lib/ctypes/util.py", line 582, in decorator
    annotations = annotationlib.get_annotations(func, eval_str=True)
  File "/home/vstinner/python/main/Lib/annotationlib.py", line 1078, in get_annotations
    else eval(_rewrite_star_unpack(value), globals, locals)
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1
    invalid return type
            ^^^^^^
SyntaxError: invalid syntax

Hopefully, the traceback starts at line 3 of ann.py which is the line creating Py_GetVersion(), so the traceback is correct and helpful!

@vstinner
vstinner merged commit d5eb312 into python:main Jul 19, 2026
56 checks passed
@vstinner

Copy link
Copy Markdown
Member Author

Merged, thanks for the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants