Get the C mapped value of the given type.
(typ: Any)
| 89 | return ctypes.py_object(data) |
| 90 | |
| 91 | def get_mapped(typ: Any) -> "Type[ctypes._CData]": |
| 92 | """Get the C mapped value of the given type.""" |
| 93 | from .c_pointer import VoidPointer |
| 94 | |
| 95 | if getattr(typ, "__origin__", None) is Callable: |
| 96 | args = list(typ.__args__) |
| 97 | res = args.pop(-1) |
| 98 | return ctypes.CFUNCTYPE(get_mapped(res), *map(get_mapped, args)) |
| 99 | |
| 100 | if type(typ) in {FunctionType, MethodType}: |
| 101 | hints = typ.__annotations__.copy() |
| 102 | try: |
| 103 | res = hints.pop("return") |
| 104 | except KeyError as e: |
| 105 | raise TypeError( |
| 106 | "return type annotation is required to convert to a C function" # noqa |
| 107 | ) from e |
| 108 | |
| 109 | args = hints.values() |
| 110 | return ctypes.CFUNCTYPE( |
| 111 | get_mapped(res) if res else None, |
| 112 | *map(get_mapped, args), |
| 113 | ) |
| 114 | |
| 115 | # VoidPointer needs to be passed here to stop circular imports |
| 116 | return {**_C_TYPES, VoidPointer: ctypes.c_void_p}.get( # type: ignore |
| 117 | typ, |
| 118 | ) or ctypes.py_object |
| 119 | |
| 120 | |
| 121 | def is_mappable(typ: Any) -> bool: |
no test coverage detected