Skip to content

Commit

Permalink
src: use dynamic version check to satisfy Mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuDartiailh committed May 28, 2024
1 parent 32e1e5f commit 3c70966
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions src/bytecode/concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,16 @@ def disassemble(cls: Type[T], lineno: Optional[int], code: bytes, offset: int) -
return cls(name, arg, lineno=lineno)

def use_cache_opcodes(self) -> int:
return (
# Not supposed to be used but we need it
dis._inline_cache_entries[self._opcode] # type: ignore
if PY313 and self._opcode in dis._inline_cache_entries
else (dis._inline_cache_entries[self._opcode] if PY311 else 0)
)
if sys.version_info >= (3, 13):
return (
dis._inline_cache_entries[self._opcode]
if self._opcode in dis._inline_cache_entries
else 0
)
elif sys.version_info >= (3, 11):
return dis._inline_cache_entries[self._opcode] # type: ignore
else:
return 0


class ExceptionTableEntry:
Expand Down Expand Up @@ -873,7 +877,7 @@ def to_code(
)
nlocals = len(self.varnames)

if PY311:
if sys.version_info >= (3, 11):
return types.CodeType(
self.argcount,
self.posonlyargcount,
Expand Down
9 changes: 6 additions & 3 deletions src/bytecode/instr.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
INTRINSIC = INTRINSIC_1OP + INTRINSIC_2OP

HASJABS = () if PY313 else _opcode.hasjabs
HASJREL = _opcode.hasjump if PY313 else _opcode.hasjrel
if sys.version_info >= (3, 13):
HASJREL = _opcode.hasjump
else:
HASJREL = _opcode.hasjrel


# Used for COMPARE_OP opcode argument
Expand Down Expand Up @@ -137,7 +140,7 @@ class Intrinsic2Op(enum.IntEnum):
# This make type checking happy but means it won't catch attempt to manipulate an unset
# statically. We would need guard on object attribute narrowed down through methods
class _UNSET(int):
instance = None
instance: Optional["_UNSET"] = None

def __new__(cls):
if cls.instance is None:
Expand Down Expand Up @@ -259,7 +262,7 @@ def _check_arg_int(arg: Any, name: str) -> TypeGuard[int]:
return True


if PY312:
if sys.version_info >= (3, 12):

def opcode_has_argument(opcode: int) -> bool:
return opcode in dis.hasarg
Expand Down
9 changes: 5 additions & 4 deletions src/bytecode/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from typing import Final

PY310 = sys.version_info >= (3, 10)
PY311 = sys.version_info >= (3, 11)
PY312 = sys.version_info >= (3, 12)
PY313 = sys.version_info >= (3, 13)
PY310: Final[bool] = sys.version_info >= (3, 10)
PY311: Final[bool] = sys.version_info >= (3, 11)
PY312: Final[bool] = sys.version_info >= (3, 12)
PY313: Final[bool] = sys.version_info >= (3, 13)

0 comments on commit 3c70966

Please sign in to comment.