From 12c22aca82e4c96aebb92f77a42efd95be2f0c9e Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 13:33:24 +0200 Subject: [PATCH 01/17] util done --- stdlib/2and3/ctypes/util.pyi | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 stdlib/2and3/ctypes/util.pyi diff --git a/stdlib/2and3/ctypes/util.pyi b/stdlib/2and3/ctypes/util.pyi new file mode 100644 index 000000000000..af0f97a95f32 --- /dev/null +++ b/stdlib/2and3/ctypes/util.pyi @@ -0,0 +1,5 @@ +# Stubs for ctypes.util + +from typing import Optional + +def find_library(name: str) -> Optional[str]: ... From 3b6405722c8ed11af49f47d7436d727c25d17133 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 14:07:37 +0200 Subject: [PATCH 02/17] __init__, 16.16.2.2 done --- stdlib/2and3/ctypes/__init__.pyi | 130 +++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 stdlib/2and3/ctypes/__init__.pyi diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi new file mode 100644 index 000000000000..1274c21b3ae9 --- /dev/null +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -0,0 +1,130 @@ +# Stubs for ctypes + +from typing import ( + Any, Optional, Type, + Generic, TypeVar, +) +import sys + +_T = TypeVar('_T') +if sys.platform == 'win32': + _DLLT = TypeVar('_DLLT', CDLL, OleDLL, WinDLL, PyDLL) +else: + _DLLT = TypeVar('_DLLT', CDLL, PyDLL) + + +RTLD_GLOBAL = ... # type: int +RTLD_LOCAL = ... # type: int +DEFAULT_MODE = ... # type: int + + +class _DLL: + def __init__(self, name: str, mode: int = ..., handle: Optional[int] = ..., + use_errno: bool = ..., use_last_error: bool = ...) -> None: ... +class CDLL(_DLL): ... +if sys.platform == 'win32': + class OleDLL(_DLL): ... + class WinDLL(_DLL): ... +class PyDLL(_DLL): + _handle = ... # type: int + _name = ... # type: str + def __init__(self, name: str, mode: int = ..., + handle: Optional[int] = ...) -> None: ... + +class LibraryLoader(Generic[_DLLT]): + def __init__(self, dlltype: Type[_DLLT]) -> None: ... + def LoadLibrary(self, name: str) -> _DLLT: ... + +cdll = ... # type: LibraryLoader[CDLL] +if sys.platform == 'win32': + windll = ... # type: LibraryLoader[WinDLL] + oledll = ... # type: LibraryLoader[OleDLL] +pydll = ... # type: LibraryLoader[PyDLL] +pythonapi = ... # type: PyDLL + + +#class _SimpleCData(Generic[_T]): +# value = ... # type: _T +# def __init__(self, value: _T) -> None: ... + +#class c_bool(_SimpleCData[int]): +# def __init__(self, value: bool) -> None: ... + +#class c_char(_SimpleCData[bytes]): ... +#class c_wchar(_SimpleCData[str]): ... + +#class c_byte(_SimpleCData[int]): ... +#class c_ubyte(_SimpleCData[int]): ... + +#class c_short(_SimpleCData[int]): ... +#class c_ushort(_SimpleCData[int]): ... + +#class c_int(_SimpleCData[int]): ... +#class c_uint(_SimpleCData[int]): ... + +#class c_long(_SimpleCData[int]): ... +#class c_ulong(_SimpleCData[int]): ... +#class c_longlong(_SimpleCData[int]): ... +#class c_ulonglong(_SimpleCData[int]): ... + +#class c_size_t(_SimpleCData[int]): ... +#class c_ssize_t(_SimpleCData[int]): ... + +#class c_float(_SimpleCData[float]): ... +#class c_double(_SimpleCData[float]): ... +#class c_longdouble(_SimpleCData[float]): ... + +#class c_char_p(_SimpleCData[Optional[bytes]]): ... +#class c_wchar_p(_SimpleCData[Optional[str]]): ... +#class c_voidp(_SimpleCData[Optional[int]]): ... + + +#def create_string_buffer(init, size=None): ... +#def c_buffer(init, size=None): ... +#def CFUNCTYPE(restype, *argtypes, **kw): ... +#def WINFUNCTYPE(restype, *argtypes, **kw): ... + +#class py_object(_SimpleCData): ... + +#c_int = ... # type: Any +#c_uint = ... # type: Any + + +#c_longdouble = ... # type: Any +#c_longlong = ... # type: Any +#c_ulonglong = ... # type: Any + +#class c_char(_SimpleCData): ... +#class c_void_p(_SimpleCData): ... + + +#class c_wchar(_SimpleCData): ... + +#def create_unicode_buffer(init, size=None): ... +#def SetPointerType(pointer, cls): ... +#def ARRAY(typ, len): ... + +#class HRESULT(_SimpleCData): ... + +#GetLastError = ... # type: Any + +#def WinError(code=None, descr=None): ... + +#memmove = ... # type: Any +#memset = ... # type: Any + +#def PYFUNCTYPE(restype, *argtypes): ... +#def cast(obj, typ): ... +#def string_at(ptr, size=-1): ... +#def wstring_at(ptr, size=-1): ... +#def DllGetClassObject(rclsid, riid, ppv): ... +#def DllCanUnloadNow(): ... + +#c_int8 = ... # type: Any +#c_uint8 = ... # type: Any +#c_int16 = ... # type: Any +#c_int32 = ... # type: Any +#c_int64 = ... # type: Any +#c_uint16 = ... # type: Any +#c_uint32 = ... # type: Any +#c_uint64 = ... # type: Any From 5b0189d9c38d5640d8e84a93d2eb7d35c38917ec Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 16:16:48 +0200 Subject: [PATCH 03/17] 16.16.2.3 done --- stdlib/2and3/ctypes/__init__.pyi | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index 1274c21b3ae9..eeffae1e7ed3 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -1,7 +1,7 @@ # Stubs for ctypes from typing import ( - Any, Optional, Type, + Any, Callable, Optional, Tuple, Type, Union, Generic, TypeVar, ) import sys @@ -43,9 +43,17 @@ pydll = ... # type: LibraryLoader[PyDLL] pythonapi = ... # type: PyDLL -#class _SimpleCData(Generic[_T]): -# value = ... # type: _T -# def __init__(self, value: _T) -> None: ... +_FRT = TypeVar('_FRT', Optional[_SimpleCData]) +class _FuncPtr(Generic[_FRT]): + restype = ... # type: Union[_FRT, Callable[[int], None]] + argtypes = ... # type: Tuple[_SimpleCData, ...] + errcheck = ... # type: Callable[[_FRT, _FuncPtr, Tuple[_SimpleCData, ...]], _SimpleCData] + +class _SimpleCData(Generic[_T]): + value = ... # type: _T + def __init__(self, value: _T) -> None: ... + +class ArgumentError(Exception): ... #class c_bool(_SimpleCData[int]): # def __init__(self, value: bool) -> None: ... From 5c7eeafe95ab8da11c371123fd62415c57435357 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 16:45:19 +0200 Subject: [PATCH 04/17] 16.16.2.4 --- stdlib/2and3/ctypes/__init__.pyi | 42 +++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index eeffae1e7ed3..f434b0945c8f 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -2,7 +2,7 @@ from typing import ( Any, Callable, Optional, Tuple, Type, Union, - Generic, TypeVar, + Generic, TypeVar, overload, ) import sys @@ -49,12 +49,45 @@ class _FuncPtr(Generic[_FRT]): argtypes = ... # type: Tuple[_SimpleCData, ...] errcheck = ... # type: Callable[[_FRT, _FuncPtr, Tuple[_SimpleCData, ...]], _SimpleCData] +class ArgumentError(Exception): ... + + +def CFUNCTYPE(restype: _SimpleCData, *argtypes: _SimpleCData, + use_errno: bool = ..., + use_last_error: bool = ...) -> Type[_FuncProto]: ... +if sys.platform == 'win32': + def WINFUNCTYPE(restype: _SimpleCData, *argtypes: _SimpleCData, + use_errno: bool = ..., + use_last_error: bool = ...) -> Type[_FuncProto]: ... +def PYFUNCTYPE(restype: _SimpleCData, *argtypes: _SimpleCData, + use_errno: bool = ..., + use_last_error: bool = ...) -> Type[_FuncProto]: ... + +_PF = Tuple[ + Tuple[int], + Tuple[int, str], + Tuple[int, str, Any], +] + +class _FuncProto(_FuncPtr): + @overload + def __init__(self, address: int) -> None: ... + @overload + def __init__(self, callable: Callable[..., Any]) -> None: ... + @overload + def __init__(self, func_spec: Tuple[Union[str, int], _DLL], + paramflags: Tuple[_PF, ...] = ...) -> None: ... + # TODO iid is a pointer to the interface identifier + @overload + def __init__(self, vtlb_index: int, name: str, + paramflags: Tuple[_PF, ...] = ..., + iid: _SimpleCData = ...) -> None: ... + + class _SimpleCData(Generic[_T]): value = ... # type: _T def __init__(self, value: _T) -> None: ... -class ArgumentError(Exception): ... - #class c_bool(_SimpleCData[int]): # def __init__(self, value: bool) -> None: ... @@ -89,8 +122,6 @@ class ArgumentError(Exception): ... #def create_string_buffer(init, size=None): ... #def c_buffer(init, size=None): ... -#def CFUNCTYPE(restype, *argtypes, **kw): ... -#def WINFUNCTYPE(restype, *argtypes, **kw): ... #class py_object(_SimpleCData): ... @@ -121,7 +152,6 @@ class ArgumentError(Exception): ... #memmove = ... # type: Any #memset = ... # type: Any -#def PYFUNCTYPE(restype, *argtypes): ... #def cast(obj, typ): ... #def string_at(ptr, size=-1): ... #def wstring_at(ptr, size=-1): ... From 3e616c7658a24428da1771b518124c6046388c70 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 18:03:24 +0200 Subject: [PATCH 05/17] fix *FUNCTYPE, fix generic --- stdlib/2and3/ctypes/__init__.pyi | 48 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index f434b0945c8f..2a2e92b2fc82 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -6,7 +6,7 @@ from typing import ( ) import sys -_T = TypeVar('_T') +_DT = TypeVar('_DT', int, bytes, str, float, None) if sys.platform == 'win32': _DLLT = TypeVar('_DLLT', CDLL, OleDLL, WinDLL, PyDLL) else: @@ -43,25 +43,29 @@ pydll = ... # type: LibraryLoader[PyDLL] pythonapi = ... # type: PyDLL -_FRT = TypeVar('_FRT', Optional[_SimpleCData]) -class _FuncPtr(Generic[_FRT]): - restype = ... # type: Union[_FRT, Callable[[int], None]] - argtypes = ... # type: Tuple[_SimpleCData, ...] - errcheck = ... # type: Callable[[_FRT, _FuncPtr, Tuple[_SimpleCData, ...]], _SimpleCData] +class _FuncPtr(Generic[_DT]): + restype = ... # type: Union[Optional[Type[_SimpleCData[_DT]]], Callable[[int], None]] + argtypes = ... # type: Tuple[Type[_SimpleCData[_DT]], ...] + errcheck = ... # type: Callable[[Optional[Type[_SimpleCData[_DT]]], _FuncPtr[_DT], Tuple[_SimpleCData[Any], ...]], _SimpleCData[_DT]] + def __call__(self, *args: Union[_SimpleCData, _cparam], + **kwargs: Union[_SimpleCData, _cparam]) -> _DT: ... class ArgumentError(Exception): ... -def CFUNCTYPE(restype: _SimpleCData, *argtypes: _SimpleCData, +def CFUNCTYPE(restype: Type[_SimpleCData[_DT]], + *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto]: ... + use_last_error: bool = ...) -> Type[_FuncProto[_DT]]: ... if sys.platform == 'win32': - def WINFUNCTYPE(restype: _SimpleCData, *argtypes: _SimpleCData, + def WINFUNCTYPE(restype: Type[_SimpleCData[_DT]], + *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto]: ... -def PYFUNCTYPE(restype: _SimpleCData, *argtypes: _SimpleCData, + use_last_error: bool = ...) -> Type[_FuncProto[_DT]]: ... +def PYFUNCTYPE(restype: Type[_SimpleCData[_DT]], + *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto]: ... + use_last_error: bool = ...) -> Type[_FuncProto[_DT]]: ... _PF = Tuple[ Tuple[int], @@ -69,24 +73,30 @@ _PF = Tuple[ Tuple[int, str, Any], ] -class _FuncProto(_FuncPtr): +class _FuncProto(Generic[_DT], _FuncPtr[_DT]): @overload def __init__(self, address: int) -> None: ... @overload - def __init__(self, callable: Callable[..., Any]) -> None: ... + def __init__(self, callable: Callable[..., _DT]) -> None: ... @overload def __init__(self, func_spec: Tuple[Union[str, int], _DLL], paramflags: Tuple[_PF, ...] = ...) -> None: ... - # TODO iid is a pointer to the interface identifier + # TODO better type: iid is a pointer to the interface identifier @overload def __init__(self, vtlb_index: int, name: str, paramflags: Tuple[_PF, ...] = ..., - iid: _SimpleCData = ...) -> None: ... + iid: _SimpleCData[Any] = ...) -> None: ... +class _cparam(Generic[_DT]): ... -class _SimpleCData(Generic[_T]): - value = ... # type: _T - def __init__(self, value: _T) -> None: ... +def addressof(obj: _SimpleCData[Any]) -> int: ... +def alignment(obj_or_type: Union[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... +def byref(obj: _SimpleCData[_DT], offset: int = ...) -> _cparam[_DT]: ... + + +class _SimpleCData(Generic[_DT]): + value = ... # type: _DT + def __init__(self, value: _DT) -> None: ... #class c_bool(_SimpleCData[int]): # def __init__(self, value: bool) -> None: ... From 040878426480221790109b110fb341a8f2bd4265 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 18:07:15 +0200 Subject: [PATCH 06/17] remove part of generic --- stdlib/2and3/ctypes/__init__.pyi | 38 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index 2a2e92b2fc82..ec5ead523100 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -6,7 +6,6 @@ from typing import ( ) import sys -_DT = TypeVar('_DT', int, bytes, str, float, None) if sys.platform == 'win32': _DLLT = TypeVar('_DLLT', CDLL, OleDLL, WinDLL, PyDLL) else: @@ -43,29 +42,29 @@ pydll = ... # type: LibraryLoader[PyDLL] pythonapi = ... # type: PyDLL -class _FuncPtr(Generic[_DT]): - restype = ... # type: Union[Optional[Type[_SimpleCData[_DT]]], Callable[[int], None]] - argtypes = ... # type: Tuple[Type[_SimpleCData[_DT]], ...] - errcheck = ... # type: Callable[[Optional[Type[_SimpleCData[_DT]]], _FuncPtr[_DT], Tuple[_SimpleCData[Any], ...]], _SimpleCData[_DT]] +class _FuncPtr: + restype = ... # type: Union[Optional[Type[_SimpleCData]], Callable[[int], None]] + argtypes = ... # type: Tuple[Type[_SimpleCData], ...] + errcheck = ... # type: Callable[[Optional[Type[_SimpleCData]], _FuncPtr, Tuple[_SimpleCData[Any], ...]], _SimpleCData] def __call__(self, *args: Union[_SimpleCData, _cparam], - **kwargs: Union[_SimpleCData, _cparam]) -> _DT: ... + **kwargs: Union[_SimpleCData, _cparam]) -> Any: ... class ArgumentError(Exception): ... -def CFUNCTYPE(restype: Type[_SimpleCData[_DT]], +def CFUNCTYPE(restype: Type[_SimpleCData], *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto[_DT]]: ... + use_last_error: bool = ...) -> Type[_FuncProto]: ... if sys.platform == 'win32': - def WINFUNCTYPE(restype: Type[_SimpleCData[_DT]], + def WINFUNCTYPE(restype: Type[_SimpleCData], *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto[_DT]]: ... -def PYFUNCTYPE(restype: Type[_SimpleCData[_DT]], + use_last_error: bool = ...) -> Type[_FuncProto]: ... +def PYFUNCTYPE(restype: Type[_SimpleCData], *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto[_DT]]: ... + use_last_error: bool = ...) -> Type[_FuncProto]: ... _PF = Tuple[ Tuple[int], @@ -73,11 +72,11 @@ _PF = Tuple[ Tuple[int, str, Any], ] -class _FuncProto(Generic[_DT], _FuncPtr[_DT]): +class _FuncProto(_FuncPtr): @overload def __init__(self, address: int) -> None: ... @overload - def __init__(self, callable: Callable[..., _DT]) -> None: ... + def __init__(self, callable: Callable[..., Any]) -> None: ... @overload def __init__(self, func_spec: Tuple[Union[str, int], _DLL], paramflags: Tuple[_PF, ...] = ...) -> None: ... @@ -87,16 +86,17 @@ class _FuncProto(Generic[_DT], _FuncPtr[_DT]): paramflags: Tuple[_PF, ...] = ..., iid: _SimpleCData[Any] = ...) -> None: ... -class _cparam(Generic[_DT]): ... +class _cparam: ... def addressof(obj: _SimpleCData[Any]) -> int: ... def alignment(obj_or_type: Union[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... -def byref(obj: _SimpleCData[_DT], offset: int = ...) -> _cparam[_DT]: ... +def byref(obj: _SimpleCData, offset: int = ...) -> _cparam: ... -class _SimpleCData(Generic[_DT]): - value = ... # type: _DT - def __init__(self, value: _DT) -> None: ... +_T = TypeVar('_T') +class _SimpleCData(Generic[_T]): + value = ... # type: _T + def __init__(self, value: _T) -> None: ... #class c_bool(_SimpleCData[int]): # def __init__(self, value: bool) -> None: ... From b13f68b9e7bfc29167fe0ab3fa7bbb25601b85cd Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 18:14:33 +0200 Subject: [PATCH 07/17] temporarly broad some type --- stdlib/2and3/ctypes/__init__.pyi | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index ec5ead523100..f08524dc87f4 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -42,12 +42,15 @@ pydll = ... # type: LibraryLoader[PyDLL] pythonapi = ... # type: PyDLL +_ECT = Callable[[Optional[Type[_SimpleCData]], + _FuncPtr, + Tuple[_SimpleCData[Any], ...]], + _SimpleCData] class _FuncPtr: restype = ... # type: Union[Optional[Type[_SimpleCData]], Callable[[int], None]] argtypes = ... # type: Tuple[Type[_SimpleCData], ...] - errcheck = ... # type: Callable[[Optional[Type[_SimpleCData]], _FuncPtr, Tuple[_SimpleCData[Any], ...]], _SimpleCData] - def __call__(self, *args: Union[_SimpleCData, _cparam], - **kwargs: Union[_SimpleCData, _cparam]) -> Any: ... + errcheck = ... # type: _ECT + def __call__(self, *args: Any, **kwargs: Any) -> Any: ... class ArgumentError(Exception): ... @@ -66,7 +69,7 @@ def PYFUNCTYPE(restype: Type[_SimpleCData], use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... -_PF = Tuple[ +_PF = Union[ Tuple[int], Tuple[int, str], Tuple[int, str, Any], From a64fcd3b0799d08cb93031a899f145ec05bc3ca3 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 18:38:45 +0200 Subject: [PATCH 08/17] 16.16.2.5 --- stdlib/2and3/ctypes/__init__.pyi | 42 +++++++++++++++++++++++--------- stdlib/2and3/ctypes/util.pyi | 3 +++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index f08524dc87f4..ec7d2cff03de 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -6,6 +6,7 @@ from typing import ( ) import sys +_T = TypeVar('_T') if sys.platform == 'win32': _DLLT = TypeVar('_DLLT', CDLL, OleDLL, WinDLL, PyDLL) else: @@ -94,9 +95,38 @@ class _cparam: ... def addressof(obj: _SimpleCData[Any]) -> int: ... def alignment(obj_or_type: Union[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... def byref(obj: _SimpleCData, offset: int = ...) -> _cparam: ... +def cast(obj: _SimpleCData[Any], type: Type[_Pointer[Any]]) -> _SimpleCData[Any]: ... +def create_string_buffer(init_or_size: Union[int, bytes], + size: Optional[int] = ...) -> Array[c_char]: ... +def create_unicode_buffer(init_or_size: Union[int, str], + size: Optional[int] = ...) -> Array[c_wchar]: ... +if sys.platform == 'win32': + def DllCanUnloadNow() -> int: ... + def DllGetClassObject(rclsid: Any, riid: Any, ppv: Any) -> int: ... # TODO not documented + def FormatError(code: int) -> str: ... + def GetLastError() -> int: ... +def get_errno() -> int: ... +if sys.platform == 'win32': + def get_last_error() -> int: ... +def memmove(dst: Union[int, _SimpleCData[Any]], + src: Union[int, _SimpleCData[Any]], + count: int) -> None: ... +def memset(dst: Union[int, _SimpleCData[Any]], + c: int, count: int) -> None: ... +def POINTER(type: Type[_SimpleCData[Any]]) -> Type[_SimpleCData[Any]]: ... # TODO need recursive typing +def pointer(obj: _SimpleCData[Any]) -> _SimpleCData[Any]: ... # TODO need recursive typing +def resize(obj: _SimpleCData[Any], size: int) -> None: ... +def set_errno(value: int) -> int: ... +if sys.platform == 'win32': + def set_last_error(value: int) -> int: ... +def sizeof(obj_or_type: Union[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... +def string_at(address: int, size: int = ...) -> bytes: ... +if sys.platform == 'win32': + def WinError(code: Optional[int] = ..., + desc: Optional[str] = ...) -> OSError: ... +def wstring_at(address: int, size: int = ...) -> str: ... -_T = TypeVar('_T') class _SimpleCData(Generic[_T]): value = ... # type: _T def __init__(self, value: _T) -> None: ... @@ -133,7 +163,6 @@ class _SimpleCData(Generic[_T]): #class c_voidp(_SimpleCData[Optional[int]]): ... -#def create_string_buffer(init, size=None): ... #def c_buffer(init, size=None): ... #class py_object(_SimpleCData): ... @@ -152,24 +181,15 @@ class _SimpleCData(Generic[_T]): #class c_wchar(_SimpleCData): ... -#def create_unicode_buffer(init, size=None): ... #def SetPointerType(pointer, cls): ... #def ARRAY(typ, len): ... #class HRESULT(_SimpleCData): ... -#GetLastError = ... # type: Any #def WinError(code=None, descr=None): ... -#memmove = ... # type: Any -#memset = ... # type: Any -#def cast(obj, typ): ... -#def string_at(ptr, size=-1): ... -#def wstring_at(ptr, size=-1): ... -#def DllGetClassObject(rclsid, riid, ppv): ... -#def DllCanUnloadNow(): ... #c_int8 = ... # type: Any #c_uint8 = ... # type: Any diff --git a/stdlib/2and3/ctypes/util.pyi b/stdlib/2and3/ctypes/util.pyi index af0f97a95f32..7077d9d2f1e9 100644 --- a/stdlib/2and3/ctypes/util.pyi +++ b/stdlib/2and3/ctypes/util.pyi @@ -1,5 +1,8 @@ # Stubs for ctypes.util from typing import Optional +import sys def find_library(name: str) -> Optional[str]: ... +if sys.platform == 'win32': + def find_msvcrt() -> Optional[str]: ... From 02338180cc1f60cd7cc14549d8356a2ac5756533 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 18:56:22 +0200 Subject: [PATCH 09/17] 16.16.2.6 --- stdlib/2and3/ctypes/__init__.pyi | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index ec7d2cff03de..bd230fe25b99 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -127,6 +127,16 @@ if sys.platform == 'win32': def wstring_at(address: int, size: int = ...) -> str: ... +class _CData: + _b_base = ... # type: int + _b_needsfree_ = ... # type: bool + _objects = ... # type: Optional[Mapping[Any, int]] + def from_buffer(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type + def from_buffer_copy(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type + def from_address(self, address: int) -> _CData: ... + def from_param(self, obj: Any) -> Union[_SimpleCData, _cparam]: ... + def in_dll(self, library: str, name: _DLL) -> _CData: ... + class _SimpleCData(Generic[_T]): value = ... # type: _T def __init__(self, value: _T) -> None: ... From dea4f9d9ec23adbe1a842df979d34265c8d4d360 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 19:09:33 +0200 Subject: [PATCH 10/17] 16.16.2.7 --- stdlib/2and3/ctypes/__init__.pyi | 81 +++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index bd230fe25b99..ca3d2ee96752 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -1,7 +1,7 @@ # Stubs for ctypes from typing import ( - Any, Callable, Optional, Tuple, Type, Union, + Any, Callable, Mapping, Optional, Tuple, Type, Union, Generic, TypeVar, overload, ) import sys @@ -137,45 +137,74 @@ class _CData: def from_param(self, obj: Any) -> Union[_SimpleCData, _cparam]: ... def in_dll(self, library: str, name: _DLL) -> _CData: ... -class _SimpleCData(Generic[_T]): + +class _SimpleCData(Generic[_T], _CData): value = ... # type: _T - def __init__(self, value: _T) -> None: ... + def __init__(self, value: _T = ...) -> None: ... + +class c_byte(_SimpleCData[int]): ... + +class c_char(_SimpleCData[bytes]): ... +class c_char_p(_SimpleCData[Optional[bytes]]): + def __init__(self, value: Union[int, bytes] = ...) -> None: ... + +class c_double(_SimpleCData[float]): ... +class c_longdouble(_SimpleCData[float]): ... +class c_float(_SimpleCData[float]): ... + +class c_int(_SimpleCData[int]): ... +class c_int8(_SimpleCData[int]): ... +class c_int16(_SimpleCData[int]): ... +class c_int32(_SimpleCData[int]): ... +class c_int64(_SimpleCData[int]): ... + +class c_long(_SimpleCData[int]): ... +class c_longlong(_SimpleCData[int]): ... + +class c_short(_SimpleCData[int]): ... + +class c_size_t(_SimpleCData[int]): ... +class c_ssize_t(_SimpleCData[int]): ... + +class c_ubyte(_SimpleCData[int]): ... + +class c_uint(_SimpleCData[int]): ... +class c_uint8(_SimpleCData[int]): ... +class c_uint16(_SimpleCData[int]): ... +class c_uint32(_SimpleCData[int]): ... +class c_uint64(_SimpleCData[int]): ... + +class c_ulong(_SimpleCData[int]): ... +class c_ulonglong(_SimpleCData[int]): ... + +class c_ushort(_SimpleCData[int]): ... + +class c_void_p(_SimpleCData[Optional[int]]): ... + +class c_wchar(_SimpleCData[str]): ... +class c_wchar_p(_SimpleCData[Optional[str]]): + def __init__(self, value: Union[int, str] = ...) -> None: ... + +class c_bool(_SimpleCData[bool]): + def __init__(self, value: bool) -> None: ... + +if sys.platform == 'win32': + class HRESULT(_SimpleCData[Any]): ... # TODO undocumented -#class c_bool(_SimpleCData[int]): -# def __init__(self, value: bool) -> None: ... +class py_object(Generic[_T], _SimpleCData[_T]): ... -#class c_char(_SimpleCData[bytes]): ... -#class c_wchar(_SimpleCData[str]): ... -#class c_byte(_SimpleCData[int]): ... -#class c_ubyte(_SimpleCData[int]): ... -#class c_short(_SimpleCData[int]): ... -#class c_ushort(_SimpleCData[int]): ... -#class c_int(_SimpleCData[int]): ... -#class c_uint(_SimpleCData[int]): ... -#class c_long(_SimpleCData[int]): ... -#class c_ulong(_SimpleCData[int]): ... -#class c_longlong(_SimpleCData[int]): ... -#class c_ulonglong(_SimpleCData[int]): ... -#class c_size_t(_SimpleCData[int]): ... -#class c_ssize_t(_SimpleCData[int]): ... -#class c_float(_SimpleCData[float]): ... -#class c_double(_SimpleCData[float]): ... -#class c_longdouble(_SimpleCData[float]): ... -#class c_char_p(_SimpleCData[Optional[bytes]]): ... -#class c_wchar_p(_SimpleCData[Optional[str]]): ... #class c_voidp(_SimpleCData[Optional[int]]): ... #def c_buffer(init, size=None): ... -#class py_object(_SimpleCData): ... #c_int = ... # type: Any #c_uint = ... # type: Any @@ -186,7 +215,6 @@ class _SimpleCData(Generic[_T]): #c_ulonglong = ... # type: Any #class c_char(_SimpleCData): ... -#class c_void_p(_SimpleCData): ... #class c_wchar(_SimpleCData): ... @@ -194,7 +222,6 @@ class _SimpleCData(Generic[_T]): #def SetPointerType(pointer, cls): ... #def ARRAY(typ, len): ... -#class HRESULT(_SimpleCData): ... #def WinError(code=None, descr=None): ... From 661ddda443be3783cc9f477bd410a9b0a0dab99c Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 19:20:38 +0200 Subject: [PATCH 11/17] 16.16.2.8 --- stdlib/2and3/ctypes/__init__.pyi | 42 +++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index ca3d2ee96752..7ca645336fbc 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -1,9 +1,10 @@ # Stubs for ctypes from typing import ( - Any, Callable, Mapping, Optional, Tuple, Type, Union, + Any, Callable, Mapping, Optional, Tuple, Type, Generic, TypeVar, overload, ) +from typing import Union as UnionT import sys _T = TypeVar('_T') @@ -48,7 +49,7 @@ _ECT = Callable[[Optional[Type[_SimpleCData]], Tuple[_SimpleCData[Any], ...]], _SimpleCData] class _FuncPtr: - restype = ... # type: Union[Optional[Type[_SimpleCData]], Callable[[int], None]] + restype = ... # type: UnionT[Optional[Type[_SimpleCData]], Callable[[int], None]] argtypes = ... # type: Tuple[Type[_SimpleCData], ...] errcheck = ... # type: _ECT def __call__(self, *args: Any, **kwargs: Any) -> Any: ... @@ -70,7 +71,7 @@ def PYFUNCTYPE(restype: Type[_SimpleCData], use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... -_PF = Union[ +_PF = UnionT[ Tuple[int], Tuple[int, str], Tuple[int, str, Any], @@ -82,7 +83,7 @@ class _FuncProto(_FuncPtr): @overload def __init__(self, callable: Callable[..., Any]) -> None: ... @overload - def __init__(self, func_spec: Tuple[Union[str, int], _DLL], + def __init__(self, func_spec: Tuple[UnionT[str, int], _DLL], paramflags: Tuple[_PF, ...] = ...) -> None: ... # TODO better type: iid is a pointer to the interface identifier @overload @@ -93,12 +94,12 @@ class _FuncProto(_FuncPtr): class _cparam: ... def addressof(obj: _SimpleCData[Any]) -> int: ... -def alignment(obj_or_type: Union[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... +def alignment(obj_or_type: UnionT[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... def byref(obj: _SimpleCData, offset: int = ...) -> _cparam: ... def cast(obj: _SimpleCData[Any], type: Type[_Pointer[Any]]) -> _SimpleCData[Any]: ... -def create_string_buffer(init_or_size: Union[int, bytes], +def create_string_buffer(init_or_size: UnionT[int, bytes], size: Optional[int] = ...) -> Array[c_char]: ... -def create_unicode_buffer(init_or_size: Union[int, str], +def create_unicode_buffer(init_or_size: UnionT[int, str], size: Optional[int] = ...) -> Array[c_wchar]: ... if sys.platform == 'win32': def DllCanUnloadNow() -> int: ... @@ -108,10 +109,10 @@ if sys.platform == 'win32': def get_errno() -> int: ... if sys.platform == 'win32': def get_last_error() -> int: ... -def memmove(dst: Union[int, _SimpleCData[Any]], - src: Union[int, _SimpleCData[Any]], +def memmove(dst: UnionT[int, _SimpleCData[Any]], + src: UnionT[int, _SimpleCData[Any]], count: int) -> None: ... -def memset(dst: Union[int, _SimpleCData[Any]], +def memset(dst: UnionT[int, _SimpleCData[Any]], c: int, count: int) -> None: ... def POINTER(type: Type[_SimpleCData[Any]]) -> Type[_SimpleCData[Any]]: ... # TODO need recursive typing def pointer(obj: _SimpleCData[Any]) -> _SimpleCData[Any]: ... # TODO need recursive typing @@ -119,7 +120,7 @@ def resize(obj: _SimpleCData[Any], size: int) -> None: ... def set_errno(value: int) -> int: ... if sys.platform == 'win32': def set_last_error(value: int) -> int: ... -def sizeof(obj_or_type: Union[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... +def sizeof(obj_or_type: UnionT[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... def string_at(address: int, size: int = ...) -> bytes: ... if sys.platform == 'win32': def WinError(code: Optional[int] = ..., @@ -134,7 +135,7 @@ class _CData: def from_buffer(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type def from_buffer_copy(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type def from_address(self, address: int) -> _CData: ... - def from_param(self, obj: Any) -> Union[_SimpleCData, _cparam]: ... + def from_param(self, obj: Any) -> UnionT[_SimpleCData, _cparam]: ... def in_dll(self, library: str, name: _DLL) -> _CData: ... @@ -146,7 +147,7 @@ class c_byte(_SimpleCData[int]): ... class c_char(_SimpleCData[bytes]): ... class c_char_p(_SimpleCData[Optional[bytes]]): - def __init__(self, value: Union[int, bytes] = ...) -> None: ... + def __init__(self, value: UnionT[int, bytes] = ...) -> None: ... class c_double(_SimpleCData[float]): ... class c_longdouble(_SimpleCData[float]): ... @@ -183,7 +184,7 @@ class c_void_p(_SimpleCData[Optional[int]]): ... class c_wchar(_SimpleCData[str]): ... class c_wchar_p(_SimpleCData[Optional[str]]): - def __init__(self, value: Union[int, str] = ...) -> None: ... + def __init__(self, value: UnionT[int, str] = ...) -> None: ... class c_bool(_SimpleCData[bool]): def __init__(self, value: bool) -> None: ... @@ -194,7 +195,20 @@ if sys.platform == 'win32': class py_object(Generic[_T], _SimpleCData[_T]): ... +class Union: + def __init__(self, *args: Any, **kw: Any) -> None: ... +class BigEndianStructure: + def __init__(self, *args: Any, **kw: Any) -> None: ... + +class LittleEndianStructure: + def __init__(self, *args: Any, **kw: Any) -> None: ... + +class Structure: + _fields_ = ... # type: Sequence[Tuple[str, Type[_SimpleCData]], Tuple[str, Type[_SimpleCData], int]] + _pack_ = ... # type: inst + _anonymous_ = ... # type: Sequence[str] + def __init__(self, *args, **kw) -> None: ... From 8eab72e86359d061e5f6c2a5984c5f266180ff1b Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 19:35:33 +0200 Subject: [PATCH 12/17] 16.16.2.9 and cleanup --- stdlib/2and3/ctypes/__init__.pyi | 73 +++++++++++++------------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index 7ca645336fbc..70212b9bbde7 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -1,7 +1,7 @@ # Stubs for ctypes from typing import ( - Any, Callable, Mapping, Optional, Tuple, Type, + Any, Callable, Iterable, Mapping, Optional, Sequence, Sized, Tuple, Type, Generic, TypeVar, overload, ) from typing import Union as UnionT @@ -99,6 +99,7 @@ def byref(obj: _SimpleCData, offset: int = ...) -> _cparam: ... def cast(obj: _SimpleCData[Any], type: Type[_Pointer[Any]]) -> _SimpleCData[Any]: ... def create_string_buffer(init_or_size: UnionT[int, bytes], size: Optional[int] = ...) -> Array[c_char]: ... +c_buffer = create_string_buffer def create_unicode_buffer(init_or_size: UnionT[int, str], size: Optional[int] = ...) -> Array[c_wchar]: ... if sys.platform == 'win32': @@ -114,13 +115,13 @@ def memmove(dst: UnionT[int, _SimpleCData[Any]], count: int) -> None: ... def memset(dst: UnionT[int, _SimpleCData[Any]], c: int, count: int) -> None: ... -def POINTER(type: Type[_SimpleCData[Any]]) -> Type[_SimpleCData[Any]]: ... # TODO need recursive typing +def POINTER(type: Type[_CData[Any]]) -> Type[_CData[Any]]: ... # TODO need recursive typing def pointer(obj: _SimpleCData[Any]) -> _SimpleCData[Any]: ... # TODO need recursive typing def resize(obj: _SimpleCData[Any], size: int) -> None: ... def set_errno(value: int) -> int: ... if sys.platform == 'win32': def set_last_error(value: int) -> int: ... -def sizeof(obj_or_type: UnionT[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... +def sizeof(obj_or_type: UnionT[_CData[Any], Type[_CData[Any]]]) -> int: ... def string_at(address: int, size: int = ...) -> bytes: ... if sys.platform == 'win32': def WinError(code: Optional[int] = ..., @@ -205,48 +206,34 @@ class LittleEndianStructure: def __init__(self, *args: Any, **kw: Any) -> None: ... class Structure: - _fields_ = ... # type: Sequence[Tuple[str, Type[_SimpleCData]], Tuple[str, Type[_SimpleCData], int]] - _pack_ = ... # type: inst + _fields_ = ... # type: Sequence[UnionT[Tuple[str, Type[_SimpleCData]], Tuple[str, Type[_SimpleCData], int]]] + _pack_ = ... # type: int _anonymous_ = ... # type: Sequence[str] - def __init__(self, *args, **kw) -> None: ... - - - - - -#class c_voidp(_SimpleCData[Optional[int]]): ... - - -#def c_buffer(init, size=None): ... - - -#c_int = ... # type: Any -#c_uint = ... # type: Any - - -#c_longdouble = ... # type: Any -#c_longlong = ... # type: Any -#c_ulonglong = ... # type: Any - -#class c_char(_SimpleCData): ... - - -#class c_wchar(_SimpleCData): ... - -#def SetPointerType(pointer, cls): ... -#def ARRAY(typ, len): ... - - + def __init__(self, *args: Any, **kw: Any) -> None: ... -#def WinError(code=None, descr=None): ... +class Array(Generic[_T], Sized, _CData): + _length_ = ... # type: int + _type_ = ... # type: Type[_T] + def __init__(self, *args: _T) -> None: ... + @overload + def __getitem__(self, i: int) -> _T: ... + @overload + def __getitem__(self, s: slice) -> List[_T]: ... + @overload + def __setitem__(self, i: int, o: _T) -> None: ... + @overload + def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ... -#c_int8 = ... # type: Any -#c_uint8 = ... # type: Any -#c_int16 = ... # type: Any -#c_int32 = ... # type: Any -#c_int64 = ... # type: Any -#c_uint16 = ... # type: Any -#c_uint32 = ... # type: Any -#c_uint64 = ... # type: Any +class _Pointer(Generic[_T], _CData): + _type_ = ... # type: Type[_T] + contents = ... # type: _T + @overload + def __getitem__(self, i: int) -> _T: ... + @overload + def __getitem__(self, s: slice) -> List[_T]: ... + @overload + def __setitem__(self, i: int, o: _T) -> None: ... + @overload + def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ... From 9ee7a388c66c63ea07a5c12c0428d1b90c171a11 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 19:40:16 +0200 Subject: [PATCH 13/17] no documentation on wintypes --- stdlib/2and3/ctypes/wintypes.pyi | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 stdlib/2and3/ctypes/wintypes.pyi diff --git a/stdlib/2and3/ctypes/wintypes.pyi b/stdlib/2and3/ctypes/wintypes.pyi new file mode 100644 index 000000000000..e69de29bb2d1 From d6c15a8cec7ac1fc944daa20f9649e9c9cb8f962 Mon Sep 17 00:00:00 2001 From: tharvik Date: Fri, 29 Jul 2016 19:48:29 +0200 Subject: [PATCH 14/17] move from _SimpleCData to _CData --- stdlib/2and3/ctypes/__init__.pyi | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index 70212b9bbde7..1d7799afe392 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -66,8 +66,8 @@ if sys.platform == 'win32': *argtypes: Type[_SimpleCData[Any]], use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... -def PYFUNCTYPE(restype: Type[_SimpleCData], - *argtypes: Type[_SimpleCData[Any]], +def PYFUNCTYPE(restype: Type[_CData], + *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... @@ -89,14 +89,14 @@ class _FuncProto(_FuncPtr): @overload def __init__(self, vtlb_index: int, name: str, paramflags: Tuple[_PF, ...] = ..., - iid: _SimpleCData[Any] = ...) -> None: ... + iid: _CData = ...) -> None: ... class _cparam: ... -def addressof(obj: _SimpleCData[Any]) -> int: ... -def alignment(obj_or_type: UnionT[_SimpleCData[Any], Type[_SimpleCData[Any]]]) -> int: ... +def addressof(obj: _CData) -> int: ... +def alignment(obj_or_type: UnionT[_CData, Type[_CData]]) -> int: ... def byref(obj: _SimpleCData, offset: int = ...) -> _cparam: ... -def cast(obj: _SimpleCData[Any], type: Type[_Pointer[Any]]) -> _SimpleCData[Any]: ... +def cast(obj: _CData, type: Type[_Pointer[Any]]) -> _CData: ... def create_string_buffer(init_or_size: UnionT[int, bytes], size: Optional[int] = ...) -> Array[c_char]: ... c_buffer = create_string_buffer @@ -110,18 +110,18 @@ if sys.platform == 'win32': def get_errno() -> int: ... if sys.platform == 'win32': def get_last_error() -> int: ... -def memmove(dst: UnionT[int, _SimpleCData[Any]], - src: UnionT[int, _SimpleCData[Any]], +def memmove(dst: UnionT[int, _CData], + src: UnionT[int, _CData], count: int) -> None: ... -def memset(dst: UnionT[int, _SimpleCData[Any]], +def memset(dst: UnionT[int, _CData], c: int, count: int) -> None: ... def POINTER(type: Type[_CData[Any]]) -> Type[_CData[Any]]: ... # TODO need recursive typing -def pointer(obj: _SimpleCData[Any]) -> _SimpleCData[Any]: ... # TODO need recursive typing -def resize(obj: _SimpleCData[Any], size: int) -> None: ... +def pointer(obj: _CData) -> _CData: ... # TODO need recursive typing +def resize(obj: _CData, size: int) -> None: ... def set_errno(value: int) -> int: ... if sys.platform == 'win32': def set_last_error(value: int) -> int: ... -def sizeof(obj_or_type: UnionT[_CData[Any], Type[_CData[Any]]]) -> int: ... +def sizeof(obj_or_type: UnionT[_CData, Type[_CData]]) -> int: ... def string_at(address: int, size: int = ...) -> bytes: ... if sys.platform == 'win32': def WinError(code: Optional[int] = ..., @@ -136,7 +136,7 @@ class _CData: def from_buffer(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type def from_buffer_copy(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type def from_address(self, address: int) -> _CData: ... - def from_param(self, obj: Any) -> UnionT[_SimpleCData, _cparam]: ... + def from_param(self, obj: Any) -> UnionT[_CData, _cparam]: ... def in_dll(self, library: str, name: _DLL) -> _CData: ... @@ -206,7 +206,7 @@ class LittleEndianStructure: def __init__(self, *args: Any, **kw: Any) -> None: ... class Structure: - _fields_ = ... # type: Sequence[UnionT[Tuple[str, Type[_SimpleCData]], Tuple[str, Type[_SimpleCData], int]]] + _fields_ = ... # type: Sequence[UnionT[Tuple[str, Type[_CData]], Tuple[str, Type[_CData], int]]] _pack_ = ... # type: int _anonymous_ = ... # type: Sequence[str] def __init__(self, *args: Any, **kw: Any) -> None: ... From fb46dd4fc38fcf085f1d9cc7a24d2a95392c72cd Mon Sep 17 00:00:00 2001 From: tharvik Date: Mon, 8 Aug 2016 14:04:17 +0200 Subject: [PATCH 15/17] base on some example --- stdlib/2and3/ctypes/__init__.pyi | 34 ++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index 1d7799afe392..45e6c247c3eb 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -12,6 +12,7 @@ if sys.platform == 'win32': _DLLT = TypeVar('_DLLT', CDLL, OleDLL, WinDLL, PyDLL) else: _DLLT = TypeVar('_DLLT', CDLL, PyDLL) +_CT = TypeVar('_CT', _CData) RTLD_GLOBAL = ... # type: int @@ -22,6 +23,7 @@ DEFAULT_MODE = ... # type: int class _DLL: def __init__(self, name: str, mode: int = ..., handle: Optional[int] = ..., use_errno: bool = ..., use_last_error: bool = ...) -> None: ... + def __getattr__(self, name: str) -> Any: ... class CDLL(_DLL): ... if sys.platform == 'win32': class OleDLL(_DLL): ... @@ -44,26 +46,26 @@ pydll = ... # type: LibraryLoader[PyDLL] pythonapi = ... # type: PyDLL -_ECT = Callable[[Optional[Type[_SimpleCData]], +_ECT = Callable[[Optional[Type[_CData]], _FuncPtr, - Tuple[_SimpleCData[Any], ...]], - _SimpleCData] + Tuple[_CData, ...]], + _CData] class _FuncPtr: - restype = ... # type: UnionT[Optional[Type[_SimpleCData]], Callable[[int], None]] - argtypes = ... # type: Tuple[Type[_SimpleCData], ...] + restype = ... # type: UnionT[Optional[Type[_CData]], Callable[[int], None]] + argtypes = ... # type: Tuple[Type[_CData], ...] errcheck = ... # type: _ECT def __call__(self, *args: Any, **kwargs: Any) -> Any: ... class ArgumentError(Exception): ... -def CFUNCTYPE(restype: Type[_SimpleCData], - *argtypes: Type[_SimpleCData[Any]], +def CFUNCTYPE(restype: Type[_CData], + *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... if sys.platform == 'win32': - def WINFUNCTYPE(restype: Type[_SimpleCData], - *argtypes: Type[_SimpleCData[Any]], + def WINFUNCTYPE(restype: Type[_CData], + *argtypes: Type[_CData], use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... def PYFUNCTYPE(restype: Type[_CData], @@ -95,7 +97,7 @@ class _cparam: ... def addressof(obj: _CData) -> int: ... def alignment(obj_or_type: UnionT[_CData, Type[_CData]]) -> int: ... -def byref(obj: _SimpleCData, offset: int = ...) -> _cparam: ... +def byref(obj: _CData, offset: int = ...) -> _cparam: ... def cast(obj: _CData, type: Type[_Pointer[Any]]) -> _CData: ... def create_string_buffer(init_or_size: UnionT[int, bytes], size: Optional[int] = ...) -> Array[c_char]: ... @@ -115,8 +117,8 @@ def memmove(dst: UnionT[int, _CData], count: int) -> None: ... def memset(dst: UnionT[int, _CData], c: int, count: int) -> None: ... -def POINTER(type: Type[_CData[Any]]) -> Type[_CData[Any]]: ... # TODO need recursive typing -def pointer(obj: _CData) -> _CData: ... # TODO need recursive typing +def POINTER(type: Type[_CT]) -> Type[_Pointer[_CT]]: ... +def pointer(obj: _CT) -> _Pointer[_CT]: ... def resize(obj: _CData, size: int) -> None: ... def set_errno(value: int) -> int: ... if sys.platform == 'win32': @@ -205,16 +207,20 @@ class BigEndianStructure: class LittleEndianStructure: def __init__(self, *args: Any, **kw: Any) -> None: ... -class Structure: +class Structure(_CData): _fields_ = ... # type: Sequence[UnionT[Tuple[str, Type[_CData]], Tuple[str, Type[_CData], int]]] _pack_ = ... # type: int _anonymous_ = ... # type: Sequence[str] def __init__(self, *args: Any, **kw: Any) -> None: ... + def __getattr__(self, name: str) -> Any: ... # TODO should be a classmethod + #def __setattr__(self, name: str, value: Any) -> None: ... class Array(Generic[_T], Sized, _CData): _length_ = ... # type: int _type_ = ... # type: Type[_T] + raw = ... # type: bytes # TODO only available with _T == c_char + value = ... # type: bytes # TODO only available with _T == c_char def __init__(self, *args: _T) -> None: ... @overload def __getitem__(self, i: int) -> _T: ... @@ -224,11 +230,13 @@ class Array(Generic[_T], Sized, _CData): def __setitem__(self, i: int, o: _T) -> None: ... @overload def __setitem__(self, s: slice, o: Iterable[_T]) -> None: ... + def __iter__(self) -> Iterable[_T]: ... class _Pointer(Generic[_T], _CData): _type_ = ... # type: Type[_T] contents = ... # type: _T + def __init__(self, arg: _T = ...) -> None: ... @overload def __getitem__(self, i: int) -> _T: ... @overload From 2283495496d347d46a4679db1bbae80fd6a9b349 Mon Sep 17 00:00:00 2001 From: tharvik Date: Mon, 8 Aug 2016 14:26:13 +0200 Subject: [PATCH 16/17] py2 done, cleanup --- stdlib/2and3/ctypes/__init__.pyi | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index 45e6c247c3eb..e102330f2111 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -51,7 +51,7 @@ _ECT = Callable[[Optional[Type[_CData]], Tuple[_CData, ...]], _CData] class _FuncPtr: - restype = ... # type: UnionT[Optional[Type[_CData]], Callable[[int], None]] + restype = ... # type: UnionT[Type[_CData], Callable[[int], None], None] argtypes = ... # type: Tuple[Type[_CData], ...] errcheck = ... # type: _ECT def __call__(self, *args: Any, **kwargs: Any) -> Any: ... @@ -69,9 +69,7 @@ if sys.platform == 'win32': use_errno: bool = ..., use_last_error: bool = ...) -> Type[_FuncProto]: ... def PYFUNCTYPE(restype: Type[_CData], - *argtypes: Type[_CData], - use_errno: bool = ..., - use_last_error: bool = ...) -> Type[_FuncProto]: ... + *argtypes: Type[_CData]) -> Type[_FuncProto]: ... _PF = UnionT[ Tuple[int], @@ -87,11 +85,10 @@ class _FuncProto(_FuncPtr): @overload def __init__(self, func_spec: Tuple[UnionT[str, int], _DLL], paramflags: Tuple[_PF, ...] = ...) -> None: ... - # TODO better type: iid is a pointer to the interface identifier @overload def __init__(self, vtlb_index: int, name: str, paramflags: Tuple[_PF, ...] = ..., - iid: _CData = ...) -> None: ... + iid: _Pointer[c_int] = ...) -> None: ... class _cparam: ... @@ -120,6 +117,8 @@ def memset(dst: UnionT[int, _CData], def POINTER(type: Type[_CT]) -> Type[_Pointer[_CT]]: ... def pointer(obj: _CT) -> _Pointer[_CT]: ... def resize(obj: _CData, size: int) -> None: ... +if sys.version_info < (3,): + def set_conversion_mode(encoding: str, errors: str) -> Tuple[str, str]: ... def set_errno(value: int) -> int: ... if sys.platform == 'win32': def set_last_error(value: int) -> int: ... @@ -135,8 +134,8 @@ class _CData: _b_base = ... # type: int _b_needsfree_ = ... # type: bool _objects = ... # type: Optional[Mapping[Any, int]] - def from_buffer(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type - def from_buffer_copy(self, source: bytearray, offset: int) -> _CData: ... # TODO source, find better type + def from_buffer(self, source: bytearray, offset: int) -> _CData: ... + def from_buffer_copy(self, source: bytearray, offset: int) -> _CData: ... def from_address(self, address: int) -> _CData: ... def from_param(self, obj: Any) -> UnionT[_CData, _cparam]: ... def in_dll(self, library: str, name: _DLL) -> _CData: ... @@ -213,7 +212,7 @@ class Structure(_CData): _anonymous_ = ... # type: Sequence[str] def __init__(self, *args: Any, **kw: Any) -> None: ... def __getattr__(self, name: str) -> Any: ... # TODO should be a classmethod - #def __setattr__(self, name: str, value: Any) -> None: ... + def __setattr__(self, name: str, value: Any) -> None: ... class Array(Generic[_T], Sized, _CData): From cac28611ad8551e255552dfb073894b933809a10 Mon Sep 17 00:00:00 2001 From: tharvik Date: Mon, 8 Aug 2016 14:41:24 +0200 Subject: [PATCH 17/17] make pytype happy --- stdlib/2and3/ctypes/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2and3/ctypes/__init__.pyi b/stdlib/2and3/ctypes/__init__.pyi index e102330f2111..f50bf8779f78 100644 --- a/stdlib/2and3/ctypes/__init__.pyi +++ b/stdlib/2and3/ctypes/__init__.pyi @@ -74,7 +74,7 @@ def PYFUNCTYPE(restype: Type[_CData], _PF = UnionT[ Tuple[int], Tuple[int, str], - Tuple[int, str, Any], + Tuple[int, str, Any] ] class _FuncProto(_FuncPtr):