Add buffer types to struct module. (#1125)

* Add buffer types to struct module.

I noticed these were missing and I made the version look alike for an easy rename into 2and3.

* Add array to WriteBuffer, add iter_unpack

* Code review comments
This commit is contained in:
David Euresti
2017-04-02 18:00:07 -04:00
committed by Jelle Zijlstra
parent 341716e63f
commit 838d1b7436
2 changed files with 55 additions and 33 deletions

View File

@@ -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, ...]]: ...

View File

@@ -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, ...]]: ...