Skip to content

Commit d5eb312

Browse files
authored
gh-153903: Fix ctypes wrap_dll_function() with string annotations (#154042)
Call get_annotations() with eval_str=True.
1 parent cae8ec8 commit d5eb312

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

Lib/ctypes/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def wrap_dll_function(dll):
579579
def decorator(func):
580580
name = func.__name__
581581
ptr = getattr(dll, name)
582-
annotations = annotationlib.get_annotations(func)
582+
annotations = annotationlib.get_annotations(func, eval_str=True)
583583

584584
try:
585585
restype = annotations.pop("return")

Lib/test/test_ctypes/test_funcptr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ def noexist():
151151
def PyObject_GetAttrString(op: ctypes.py_object, attr: ctypes.c_char_p):
152152
pass
153153

154+
def test_wrap_dll_function_str_ann(self):
155+
from test.test_ctypes import wrap_str_ann
156+
version = wrap_str_ann.Py_GetVersion()
157+
self.assertIsInstance(version, bytes)
158+
154159

155160
if __name__ == '__main__':
156161
unittest.main()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import annotations
2+
import ctypes.util
3+
4+
def test_ann() -> ctypes.c_char_p:
5+
...
6+
7+
# Check that "from __future__ import annotations" works as expected
8+
if not isinstance(test_ann.__annotations__['return'], str):
9+
raise Exception("annotations must be strings")
10+
11+
@ctypes.util.wrap_dll_function(ctypes.pythonapi)
12+
def Py_GetVersion() -> ctypes.c_char_p:
13+
...

0 commit comments

Comments
 (0)