diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index b035bfb99feee0a..c65fc3032fdfad4 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -579,7 +579,7 @@ def wrap_dll_function(dll): def decorator(func): name = func.__name__ ptr = getattr(dll, name) - annotations = annotationlib.get_annotations(func) + annotations = annotationlib.get_annotations(func, eval_str=True) try: restype = annotations.pop("return") diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 94d03ad553d2227..86699ad81098275 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -151,6 +151,11 @@ def noexist(): def PyObject_GetAttrString(op: ctypes.py_object, attr: ctypes.c_char_p): pass + def test_wrap_dll_function_str_ann(self): + from test.test_ctypes import wrap_str_ann + version = wrap_str_ann.Py_GetVersion() + self.assertIsInstance(version, bytes) + if __name__ == '__main__': unittest.main() diff --git a/Lib/test/test_ctypes/wrap_str_ann.py b/Lib/test/test_ctypes/wrap_str_ann.py new file mode 100644 index 000000000000000..aa114a640c3a990 --- /dev/null +++ b/Lib/test/test_ctypes/wrap_str_ann.py @@ -0,0 +1,13 @@ +from __future__ import annotations +import ctypes.util + +def test_ann() -> ctypes.c_char_p: + ... + +# Check that "from __future__ import annotations" works as expected +if not isinstance(test_ann.__annotations__['return'], str): + raise Exception("annotations must be strings") + +@ctypes.util.wrap_dll_function(ctypes.pythonapi) +def Py_GetVersion() -> ctypes.c_char_p: + ...