diff --git a/stdlib/2/struct.pyi b/stdlib/2/struct.pyi index 213b61855..be4474ecd 100644 --- a/stdlib/2/struct.pyi +++ b/stdlib/2/struct.pyi @@ -1,28 +1,40 @@ -# Stubs for struct for Python 2.7 -# Based on https://docs.python.org/2/library/struct.html +# Stubs for struct -from typing import Any, Tuple +# Based on http://docs.python.org/3.2/library/struct.html +# Based on http://docs.python.org/2/library/struct.html + +import sys +from typing import Any, Tuple, Text, Union, Iterator +from array import array class error(Exception): ... -def pack(fmt: str, *v: Any) -> str: ... -# TODO buffer type -def pack_into(fmt: str, buffer: Any, offset: int, *v: Any) -> None: ... +_FmtType = Union[bytes, Text] +if sys.version_info >= (3,): + _BufferType = Union[bytes, bytearray, memoryview] + _WriteBufferType = Union[array, bytearray, memoryview] +else: + _BufferType = Union[bytes, bytearray, buffer, memoryview] + _WriteBufferType = Union[array[Any], bytearray, buffer, memoryview] -# TODO buffer type -def unpack(fmt: str, buffer: Any) -> Tuple[Any, ...]: ... -def unpack_from(fmt: str, buffer: Any, offset: int = ...) -> Tuple[Any, ...]: ... +def pack(fmt: _FmtType, *v: Any) -> bytes: ... +def pack_into(fmt: _FmtType, buffer: _WriteBufferType, offset: int, *v: Any) -> None: ... +def unpack(fmt: _FmtType, buffer: _BufferType) -> Tuple[Any, ...]: ... +def unpack_from(fmt: _FmtType, buffer: _BufferType, offset: int = ...) -> Tuple[Any, ...]: ... +if sys.version_info >= (3, 4): + def iter_unpack(fmt: _FmtType, buffer: _BufferType) -> Iterator[Tuple[Any, ...]]: ... -def calcsize(fmt: str) -> int: ... +def calcsize(fmt: _FmtType) -> int: ... class Struct: - format = ... # type: str + format = ... # type: bytes size = ... # type: int - def __init__(self, format: str) -> None: ... + def __init__(self, format: _FmtType) -> None: ... - def pack(self, *v: Any) -> str: ... - # TODO buffer type - def pack_into(self, buffer: Any, offset: int, *v: Any) -> None: ... - def unpack(self, buffer: Any) -> Tuple[Any, ...]: ... - def unpack_from(self, buffer: Any, offset: int = ...) -> Tuple[Any, ...]: ... + def pack(self, *v: Any) -> bytes: ... + def pack_into(self, buffer: _WriteBufferType, offset: int, *v: Any) -> None: ... + def unpack(self, buffer: _BufferType) -> Tuple[Any, ...]: ... + def unpack_from(self, buffer: _BufferType, offset: int = ...) -> Tuple[Any, ...]: ... + if sys.version_info >= (3, 4): + def iter_unpack(self, buffer: _BufferType) -> Iterator[Tuple[Any, ...]]: ... diff --git a/stdlib/3/struct.pyi b/stdlib/3/struct.pyi index f5396102d..be4474ecd 100644 --- a/stdlib/3/struct.pyi +++ b/stdlib/3/struct.pyi @@ -1,30 +1,40 @@ # Stubs for struct # Based on http://docs.python.org/3.2/library/struct.html +# Based on http://docs.python.org/2/library/struct.html -from typing import overload, Any, Tuple +import sys +from typing import Any, Tuple, Text, Union, Iterator +from array import array class error(Exception): ... -def pack(fmt: str, *v: Any) -> bytes: ... -# TODO buffer type -def pack_into(fmt: str, buffer: Any, offset: int, *v: Any) -> None: ... +_FmtType = Union[bytes, Text] +if sys.version_info >= (3,): + _BufferType = Union[bytes, bytearray, memoryview] + _WriteBufferType = Union[array, bytearray, memoryview] +else: + _BufferType = Union[bytes, bytearray, buffer, memoryview] + _WriteBufferType = Union[array[Any], bytearray, buffer, memoryview] -# TODO buffer type -def unpack(fmt: str, buffer: Any) -> Tuple[Any, ...]: ... -def unpack_from(fmt: str, buffer: Any, offset: int = ...) -> Tuple[Any, ...]: ... +def pack(fmt: _FmtType, *v: Any) -> bytes: ... +def pack_into(fmt: _FmtType, buffer: _WriteBufferType, offset: int, *v: Any) -> None: ... +def unpack(fmt: _FmtType, buffer: _BufferType) -> Tuple[Any, ...]: ... +def unpack_from(fmt: _FmtType, buffer: _BufferType, offset: int = ...) -> Tuple[Any, ...]: ... +if sys.version_info >= (3, 4): + def iter_unpack(fmt: _FmtType, buffer: _BufferType) -> Iterator[Tuple[Any, ...]]: ... -def calcsize(fmt: str) -> int: ... +def calcsize(fmt: _FmtType) -> int: ... class Struct: - format = b'' - size = 0 + format = ... # type: bytes + size = ... # type: int - def __init__(self, format: str) -> None: ... + def __init__(self, format: _FmtType) -> None: ... def pack(self, *v: Any) -> bytes: ... - # TODO buffer type - def pack_into(self, buffer: Any, offset: int, *v: Any) -> None: ... - # TODO buffer type - def unpack(self, buffer: Any) -> Tuple[Any, ...]: ... - def unpack_from(self, buffer: Any, offset: int = ...) -> Tuple[Any, ...]: ... + def pack_into(self, buffer: _WriteBufferType, offset: int, *v: Any) -> None: ... + def unpack(self, buffer: _BufferType) -> Tuple[Any, ...]: ... + def unpack_from(self, buffer: _BufferType, offset: int = ...) -> Tuple[Any, ...]: ... + if sys.version_info >= (3, 4): + def iter_unpack(self, buffer: _BufferType) -> Iterator[Tuple[Any, ...]]: ...