Call get_annotations() with eval_str=True.
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")
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()
--- /dev/null
+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:
+ ...