diff --git a/stdlib/3/dis.pyi b/stdlib/3/dis.pyi new file mode 100644 index 000000000..8c08946ad --- /dev/null +++ b/stdlib/3/dis.pyi @@ -0,0 +1,72 @@ +from typing import (List, Union, Iterator, Iterable, Tuple, Optional, Dict, + Any, IO, NamedTuple) + +from opcode import (hasconst, hasname, hasjrel, hasjabs, haslocal, hascompare, + hasfree, hasnargs, cmp_op, opname , opmap , HAVE_ARGUMENT, + EXTENDED_ARG, stack_effect) + +import types + +_have_code = Union[types.MethodType, types.FunctionType, types.CodeType, type] +_have_code_or_string = Union[_have_code, str, bytes] + + +class Instruction(NamedTuple("Instruction", [ + ('opname', str), + ('opcode', int), + ('arg', Optional[int]), + ('argval', Any), + ('argrepr', str), + ('offset', int), + ('starts_line', Optional[int]), + ('is_jump_target', bool) + ])): + # ad-hoc - seems to be an error in the NamedTuple type hint + # TODO: mypy issue #1076 + _fields = ... # type: List[str] + _source = ... # type: str + def _replace(self, *, opname: str = ..., opcode: int = ..., + arg: Optional[int] = ..., argval: Any = ..., argrepr: str = ..., + offset: int = ..., starts_line: Optional[int] = ..., + is_jump_target: bool = ...) -> Instruction: ... + def _asdict(self) -> Dict[str, Any]: ... + @staticmethod + def _make(iterable: Iterable[Any]) -> Instruction: ... + + +# if sys.version_info >= (3, 4): +class Bytecode: + codeobj = ... # type: types.CodeType + first_line = ... # type: int + def __init__(self, x: _have_code_or_string, *, first_line: int=..., + current_offset: int=...) -> None: ... + def __iter__(self) -> Iterator[Instruction]: ... + def __repr__(self) -> str: ... + def info(self) -> str: ... + def dis(self) -> str: ... + + @classmethod + def from_traceback(cls, tb: types.TracebackType) -> Bytecode: ... + + +COMPILER_FLAG_NAMES = ... # type: Dict[int, str] + + +def pretty_flags(flags: int) -> str: ... +def findlabels(code: _have_code) -> List[int]: ... +def findlinestarts(code: _have_code) -> Iterator[Tuple[int, int]]: ... + +# Signature changes are not allowed by mypy +# 'All conditional function variants must have identical signatures' +# TODO: mypy issue #698 + +# if sys.version_info >= (3, 2): +def code_info(x: _have_code_or_string) -> str: ... + +# `file` parameter requires sys.version_info >= (3, 4): +def dis(x: _have_code_or_string = ..., *, file = ...) -> None: ... +def distb(tb: types.TracebackType = ..., *, file: IO[str] = ...) -> None: ... +def disassemble(co: _have_code, lasti: int = ..., *, file = ...) -> None: ... +def show_code(co: _have_code, *, file: IO[str]=...) -> None: ... + +def get_instructions(x: _have_code, *, first_line: int = ...) -> Iterator[Instruction]: ... diff --git a/stdlib/3/opcode.pyi b/stdlib/3/opcode.pyi new file mode 100644 index 000000000..e577a9ff5 --- /dev/null +++ b/stdlib/3/opcode.pyi @@ -0,0 +1,18 @@ +from typing import List, Dict, Sequence + +cmp_op = ... # type: Sequence[str] +hasconst = ... # type: List[int] +hasname = ... # type: List[int] +hasjrel = ... # type: List[int] +hasjabs = ... # type: List[int] +haslocal = ... # type: List[int] +hascompare = ... # type: List[int] +hasfree = ... # type: List[int] +opname = ... # type: List[str] + +opmap = ... # Dict[str, int] +HAVE_ARGUMENT = ... # type: int +EXTENDED_ARG = ... # type: int +hasnargs = ... # type: List[int] + +def stack_effect(opcode: int, oparg: int = ...) -> int: ...