图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.3 KiB

  1. from typing import Any, TypeVar, Type, overload, Optional, Generic
  2. import ctypes as ct
  3. from numpy import ndarray
  4. _CastT = TypeVar("_CastT", bound=ct._CanCastTo) # Copied from `ctypes.cast`
  5. _CT = TypeVar("_CT", bound=ct._CData)
  6. _PT = TypeVar("_PT", bound=Optional[int])
  7. # TODO: Let the likes of `shape_as` and `strides_as` return `None`
  8. # for 0D arrays once we've got shape-support
  9. class _ctypes(Generic[_PT]):
  10. @overload
  11. def __new__(cls, array: ndarray[Any, Any], ptr: None = ...) -> _ctypes[None]: ...
  12. @overload
  13. def __new__(cls, array: ndarray[Any, Any], ptr: _PT) -> _ctypes[_PT]: ...
  14. # NOTE: In practice `shape` and `strides` return one of the concrete
  15. # platform dependant array-types (`c_int`, `c_long` or `c_longlong`)
  16. # corresponding to C's `int_ptr_t`, as determined by `_getintp_ctype`
  17. # TODO: Hook this in to the mypy plugin so that a more appropiate
  18. # `ctypes._SimpleCData[int]` sub-type can be returned
  19. @property
  20. def data(self) -> _PT: ...
  21. @property
  22. def shape(self) -> ct.Array[ct.c_int64]: ...
  23. @property
  24. def strides(self) -> ct.Array[ct.c_int64]: ...
  25. @property
  26. def _as_parameter_(self) -> ct.c_void_p: ...
  27. def data_as(self, obj: Type[_CastT]) -> _CastT: ...
  28. def shape_as(self, obj: Type[_CT]) -> ct.Array[_CT]: ...
  29. def strides_as(self, obj: Type[_CT]) -> ct.Array[_CT]: ...