|
| 1 | +import _ctypes |
| 2 | +import ctypes as ct |
| 3 | +from typing import Any, overload |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +# |
| 8 | +@overload |
| 9 | +def dtype_from_ctypes_type(t: type[_ctypes.Array[Any] | _ctypes.Structure]) -> np.dtype[np.void]: ... |
| 10 | +@overload |
| 11 | +def dtype_from_ctypes_type(t: type[ct.c_bool]) -> np.dtype[np.bool]: ... |
| 12 | +@overload |
| 13 | +def dtype_from_ctypes_type(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ... |
| 14 | +@overload |
| 15 | +def dtype_from_ctypes_type(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ... |
| 16 | +@overload |
| 17 | +def dtype_from_ctypes_type(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ... |
| 18 | +@overload |
| 19 | +def dtype_from_ctypes_type(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ... |
| 20 | +@overload |
| 21 | +def dtype_from_ctypes_type(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ... |
| 22 | +@overload |
| 23 | +def dtype_from_ctypes_type(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ... |
| 24 | +@overload |
| 25 | +def dtype_from_ctypes_type(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ... |
| 26 | +@overload |
| 27 | +def dtype_from_ctypes_type(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ... |
| 28 | +@overload |
| 29 | +def dtype_from_ctypes_type(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ... |
| 30 | +@overload |
| 31 | +def dtype_from_ctypes_type(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ... |
| 32 | +@overload |
| 33 | +def dtype_from_ctypes_type(t: type[ct.c_float]) -> np.dtype[np.float32]: ... |
| 34 | +@overload |
| 35 | +def dtype_from_ctypes_type(t: type[ct.c_double]) -> np.dtype[np.float64]: ... |
| 36 | +@overload |
| 37 | +def dtype_from_ctypes_type(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ... |
| 38 | +@overload |
| 39 | +def dtype_from_ctypes_type(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ... |
| 40 | +@overload |
| 41 | +def dtype_from_ctypes_type(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ... |
| 42 | + |
| 43 | +# NOTE: the complex ctypes on python>=3.14 are not yet supported at runtim, see |
| 44 | +# https://github.com/numpy/numpy/issues/28360 |
| 45 | + |
| 46 | +# |
| 47 | +def _from_ctypes_array(t: type[_ctypes.Array[Any]]) -> np.dtype[np.void]: ... |
| 48 | +def _from_ctypes_structure(t: type[_ctypes.Structure]) -> np.dtype[np.void]: ... |
| 49 | +def _from_ctypes_union(t: type[_ctypes.Union]) -> np.dtype[np.void]: ... |
| 50 | + |
| 51 | +# keep in sync with `dtype_from_ctypes_type` (minus the first overload) |
| 52 | +@overload |
| 53 | +def _from_ctypes_scalar(t: type[ct.c_bool]) -> np.dtype[np.bool]: ... |
| 54 | +@overload |
| 55 | +def _from_ctypes_scalar(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ... |
| 56 | +@overload |
| 57 | +def _from_ctypes_scalar(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ... |
| 58 | +@overload |
| 59 | +def _from_ctypes_scalar(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ... |
| 60 | +@overload |
| 61 | +def _from_ctypes_scalar(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ... |
| 62 | +@overload |
| 63 | +def _from_ctypes_scalar(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ... |
| 64 | +@overload |
| 65 | +def _from_ctypes_scalar(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ... |
| 66 | +@overload |
| 67 | +def _from_ctypes_scalar(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ... |
| 68 | +@overload |
| 69 | +def _from_ctypes_scalar(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ... |
| 70 | +@overload |
| 71 | +def _from_ctypes_scalar(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ... |
| 72 | +@overload |
| 73 | +def _from_ctypes_scalar(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ... |
| 74 | +@overload |
| 75 | +def _from_ctypes_scalar(t: type[ct.c_float]) -> np.dtype[np.float32]: ... |
| 76 | +@overload |
| 77 | +def _from_ctypes_scalar(t: type[ct.c_double]) -> np.dtype[np.float64]: ... |
| 78 | +@overload |
| 79 | +def _from_ctypes_scalar(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ... |
| 80 | +@overload |
| 81 | +def _from_ctypes_scalar(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ... |
| 82 | +@overload |
| 83 | +def _from_ctypes_scalar(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ... |
0 commit comments