Skip to content

Commit 17f7d3d

Browse files
tharvikmatthiaskramm
authored andcommitted
Add ctypes (#454)
* util done * __init__, 16.16.2.2 done * 16.16.2.3 done * 16.16.2.4 * fix *FUNCTYPE, fix generic * remove part of generic * temporarly broad some type * 16.16.2.5 * 16.16.2.6 * 16.16.2.7 * 16.16.2.8 * 16.16.2.9 and cleanup * no documentation on wintypes * move from _SimpleCData to _CData * base on some example * py2 done, cleanup * make pytype happy
1 parent 0989cb1 commit 17f7d3d

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed

stdlib/2and3/ctypes/__init__.pyi

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
# Stubs for ctypes
2+
3+
from typing import (
4+
Any, Callable, Iterable, Mapping, Optional, Sequence, Sized, Tuple, Type,
5+
Generic, TypeVar, overload,
6+
)
7+
from typing import Union as UnionT
8+
import sys
9+
10+
_T = TypeVar('_T')
11+
if sys.platform == 'win32':
12+
_DLLT = TypeVar('_DLLT', CDLL, OleDLL, WinDLL, PyDLL)
13+
else:
14+
_DLLT = TypeVar('_DLLT', CDLL, PyDLL)
15+
_CT = TypeVar('_CT', _CData)
16+
17+
18+
RTLD_GLOBAL = ... # type: int
19+
RTLD_LOCAL = ... # type: int
20+
DEFAULT_MODE = ... # type: int
21+
22+
23+
class _DLL:
24+
def __init__(self, name: str, mode: int = ..., handle: Optional[int] = ...,
25+
use_errno: bool = ..., use_last_error: bool = ...) -> None: ...
26+
def __getattr__(self, name: str) -> Any: ...
27+
class CDLL(_DLL): ...
28+
if sys.platform == 'win32':
29+
class OleDLL(_DLL): ...
30+
class WinDLL(_DLL): ...
31+
class PyDLL(_DLL):
32+
_handle = ... # type: int
33+
_name = ... # type: str
34+
def __init__(self, name: str, mode: int = ...,
35+
handle: Optional[int] = ...) -> None: ...
36+
37+
class LibraryLoader(Generic[_DLLT]):
38+
def __init__(self, dlltype: Type[_DLLT]) -> None: ...
39+
def LoadLibrary(self, name: str) -> _DLLT: ...
40+
41+
cdll = ... # type: LibraryLoader[CDLL]
42+
if sys.platform == 'win32':
43+
windll = ... # type: LibraryLoader[WinDLL]
44+
oledll = ... # type: LibraryLoader[OleDLL]
45+
pydll = ... # type: LibraryLoader[PyDLL]
46+
pythonapi = ... # type: PyDLL
47+
48+
49+
_ECT = Callable[[Optional[Type[_CData]],
50+
_FuncPtr,
51+
Tuple[_CData, ...]],
52+
_CData]
53+
class _FuncPtr:
54+
restype = ... # type: UnionT[Type[_CData], Callable[[int], None], None]
55+
argtypes = ... # type: Tuple[Type[_CData], ...]
56+
errcheck = ... # type: _ECT
57+
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
58+
59+
class ArgumentError(Exception): ...
60+
61+
62+
def CFUNCTYPE(restype: Type[_CData],
63+
*argtypes: Type[_CData],
64+
use_errno: bool = ...,
65+
use_last_error: bool = ...) -> Type[_FuncProto]: ...
66+
if sys.platform == 'win32':
67+
def WINFUNCTYPE(restype: Type[_CData],
68+
*argtypes: Type[_CData],
69+
use_errno: bool = ...,
70+
use_last_error: bool = ...) -> Type[_FuncProto]: ...
71+
def PYFUNCTYPE(restype: Type[_CData],
72+
*argtypes: Type[_CData]) -> Type[_FuncProto]: ...
73+
74+
_PF = UnionT[
75+
Tuple[int],
76+
Tuple[int, str],
77+
Tuple[int, str, Any]
78+
]
79+
80+
class _FuncProto(_FuncPtr):
81+
@overload
82+
def __init__(self, address: int) -> None: ...
83+
@overload
84+
def __init__(self, callable: Callable[..., Any]) -> None: ...
85+
@overload
86+
def __init__(self, func_spec: Tuple[UnionT[str, int], _DLL],
87+
paramflags: Tuple[_PF, ...] = ...) -> None: ...
88+
@overload
89+
def __init__(self, vtlb_index: int, name: str,
90+
paramflags: Tuple[_PF, ...] = ...,
91+
iid: _Pointer[c_int] = ...) -> None: ...
92+
93+
class _cparam: ...
94+
95+
def addressof(obj: _CData) -> int: ...
96+
def alignment(obj_or_type: UnionT[_CData, Type[_CData]]) -> int: ...
97+
def byref(obj: _CData, offset: int = ...) -> _cparam: ...
98+
def cast(obj: _CData, type: Type[_Pointer[Any]]) -> _CData: ...
99+
def create_string_buffer(init_or_size: UnionT[int, bytes],
100+
size: Optional[int] = ...) -> Array[c_char]: ...
101+
c_buffer = create_string_buffer
102+
def create_unicode_buffer(init_or_size: UnionT[int, str],
103+
size: Optional[int] = ...) -> Array[c_wchar]: ...
104+
if sys.platform == 'win32':
105+
def DllCanUnloadNow() -> int: ...
106+
def DllGetClassObject(rclsid: Any, riid: Any, ppv: Any) -> int: ... # TODO not documented
107+
def FormatError(code: int) -> str: ...
108+
def GetLastError() -> int: ...
109+
def get_errno() -> int: ...
110+
if sys.platform == 'win32':
111+
def get_last_error() -> int: ...
112+
def memmove(dst: UnionT[int, _CData],
113+
src: UnionT[int, _CData],
114+
count: int) -> None: ...
115+
def memset(dst: UnionT[int, _CData],
116+
c: int, count: int) -> None: ...
117+
def POINTER(type: Type[_CT]) -> Type[_Pointer[_CT]]: ...
118+
def pointer(obj: _CT) -> _Pointer[_CT]: ...
119+
def resize(obj: _CData, size: int) -> None: ...
120+
if sys.version_info < (3,):
121+
def set_conversion_mode(encoding: str, errors: str) -> Tuple[str, str]: ...
122+
def set_errno(value: int) -> int: ...
123+
if sys.platform == 'win32':
124+
def set_last_error(value: int) -> int: ...
125+
def sizeof(obj_or_type: UnionT[_CData, Type[_CData]]) -> int: ...
126+
def string_at(address: int, size: int = ...) -> bytes: ...
127+
if sys.platform == 'win32':
128+
def WinError(code: Optional[int] = ...,
129+
desc: Optional[str] = ...) -> OSError: ...
130+
def wstring_at(address: int, size: int = ...) -> str: ...
131+
132+
133+
class _CData:
134+
_b_base = ... # type: int
135+
_b_needsfree_ = ... # type: bool
136+
_objects = ... # type: Optional[Mapping[Any, int]]
137+
def from_buffer(self, source: bytearray, offset: int) -> _CData: ...
138+
def from_buffer_copy(self, source: bytearray, offset: int) -> _CData: ...
139+
def from_address(self, address: int) -> _CData: ...
140+
def from_param(self, obj: Any) -> UnionT[_CData, _cparam]: ...
141+
def in_dll(self, library: str, name: _DLL) -> _CData: ...
142+
143+
144+
class _SimpleCData(Generic[_T], _CData):
145+
value = ... # type: _T
146+
def __init__(self, value: _T = ...) -> None: ...
147+
148+
class c_byte(_SimpleCData[int]): ...
149+
150+
class c_char(_SimpleCData[bytes]): ...
151+
class c_char_p(_SimpleCData[Optional[bytes]]):
152+
def __init__(self, value: UnionT[int, bytes] = ...) -> None: ...
153+
154+
class c_double(_SimpleCData[float]): ...
155+
class c_longdouble(_SimpleCData[float]): ...
156+
class c_float(_SimpleCData[float]): ...
157+
158+
class c_int(_SimpleCData[int]): ...
159+
class c_int8(_SimpleCData[int]): ...
160+
class c_int16(_SimpleCData[int]): ...
161+
class c_int32(_SimpleCData[int]): ...
162+
class c_int64(_SimpleCData[int]): ...
163+
164+
class c_long(_SimpleCData[int]): ...
165+
class c_longlong(_SimpleCData[int]): ...
166+
167+
class c_short(_SimpleCData[int]): ...
168+
169+
class c_size_t(_SimpleCData[int]): ...
170+
class c_ssize_t(_SimpleCData[int]): ...
171+
172+
class c_ubyte(_SimpleCData[int]): ...
173+
174+
class c_uint(_SimpleCData[int]): ...
175+
class c_uint8(_SimpleCData[int]): ...
176+
class c_uint16(_SimpleCData[int]): ...
177+
class c_uint32(_SimpleCData[int]): ...
178+
class c_uint64(_SimpleCData[int]): ...
179+
180+
class c_ulong(_SimpleCData[int]): ...
181+
class c_ulonglong(_SimpleCData[int]): ...
182+
183+
class c_ushort(_SimpleCData[int]): ...
184+
185+
class c_void_p(_SimpleCData[Optional[int]]): ...
186+
187+
class c_wchar(_SimpleCData[str]): ...
188+
class c_wchar_p(_SimpleCData[Optional[str]]):
189+
def __init__(self, value: UnionT[int, str] = ...) -> None: ...
190+
191+
class c_bool(_SimpleCData[bool]):
192+
def __init__(self, value: bool) -> None: ...
193+
194+
if sys.platform == 'win32':
195+
class HRESULT(_SimpleCData[Any]): ... # TODO undocumented
196+
197+
class py_object(Generic[_T], _SimpleCData[_T]): ...
198+
199+
200+
class Union:
201+
def __init__(self, *args: Any, **kw: Any) -> None: ...
202+
203+
class BigEndianStructure:
204+
def __init__(self, *args: Any, **kw: Any) -> None: ...
205+
206+
class LittleEndianStructure:
207+
def __init__(self, *args: Any, **kw: Any) -> None: ...
208+
209+
class Structure(_CData):
210+
_fields_ = ... # type: Sequence[UnionT[Tuple[str, Type[_CData]], Tuple[str, Type[_CData], int]]]
211+
_pack_ = ... # type: int
212+
_anonymous_ = ... # type: Sequence[str]
213+
def __init__(self, *args: Any, **kw: Any) -> None: ...
214+
def __getattr__(self, name: str) -> Any: ... # TODO should be a classmethod
215+
def __setattr__(self, name: str, value: Any) -> None: ...
216+
217+
218+
class Array(Generic[_T], Sized, _CData):
219+
_length_ = ... # type: int
220+
_type_ = ... # type: Type[_T]
221+
raw = ... # type: bytes # TODO only available with _T == c_char
222+
value = ... # type: bytes # TODO only available with _T == c_char
223+
def __init__(self, *args: _T) -> None: ...
224+
@overload
225+
def __getitem__(self, i: int) -> _T: ...
226+
@overload
227+
def __getitem__(self, s: slice) -> List[_T]: ...
228+
@overload
229+
def __setitem__(self, i: int, o: _T) -> None: ...
230+
@overload
231+
def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...
232+
def __iter__(self) -> Iterable[_T]: ...
233+
234+
235+
class _Pointer(Generic[_T], _CData):
236+
_type_ = ... # type: Type[_T]
237+
contents = ... # type: _T
238+
def __init__(self, arg: _T = ...) -> None: ...
239+
@overload
240+
def __getitem__(self, i: int) -> _T: ...
241+
@overload
242+
def __getitem__(self, s: slice) -> List[_T]: ...
243+
@overload
244+
def __setitem__(self, i: int, o: _T) -> None: ...
245+
@overload
246+
def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ...

stdlib/2and3/ctypes/util.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Stubs for ctypes.util
2+
3+
from typing import Optional
4+
import sys
5+
6+
def find_library(name: str) -> Optional[str]: ...
7+
if sys.platform == 'win32':
8+
def find_msvcrt() -> Optional[str]: ...

stdlib/2and3/ctypes/wintypes.pyi

Whitespace-only changes.

0 commit comments

Comments
 (0)