Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion pytest/test_instructions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
"""xdis.bytecode testing"""
import dis
import sys

import pytest
from xdis import IS_GRAAL, IS_PYPY
from xdis.bytecode import Bytecode
from xdis.op_imports import get_opcode_module
from xdis.version_info import PYTHON_VERSION_TRIPLE
from xdis.version_info import PYTHON_IMPLEMENTATION, PYTHON_VERSION_TRIPLE


@pytest.mark.skipif(
IS_PYPY or PYTHON_VERSION_TRIPLE[:2] not in ((3, 14), (3, 15)),
reason="LOAD_COMMON_CONSTANT is specific to CPython 3.14 and 3.15",
)
def test_load_common_constant_argval_matches_dis() -> None:
codes = [
compile(source, "<test>", "exec")
for source in (
"assert False",
'result = {"success": True, "failure": False}',
)
]

native = [
(inst.arg, inst.argval, inst.argrepr)
for code in codes
for inst in dis.get_instructions(code)
if inst.opname == "LOAD_COMMON_CONSTANT"
]
cross = [
(inst.arg, inst.argval, inst.argrepr)
for code in codes
for inst in Bytecode(
code, get_opcode_module(PYTHON_VERSION_TRIPLE, PYTHON_IMPLEMENTATION)
)
if inst.opname == "LOAD_COMMON_CONSTANT"
]

assert cross == native


def extended_arg_fn36() -> int:
Expand Down
2 changes: 2 additions & 0 deletions xdis/bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ def get_logical_instruction_at_offset(
)
if hasattr(opc, "opcode_arg_fmt") and opname in opc.opcode_arg_fmt:
argrepr = opc.opcode_arg_fmt[opname](arg)
if hasattr(opc, "opcode_arg_val") and opname in opc.opcode_arg_val:
argval = opc.opcode_arg_val[opname](arg)
else:
if fixed_length_instructions:
i += 1
Expand Down
37 changes: 23 additions & 14 deletions xdis/opcodes/opcode_3x/opcode_315.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,24 +373,29 @@ def extended_BINARY_OP_315(opc, instructions):


_common_constants = (
"AssertionError",
"NotImplementedError",
"tuple",
"all",
"any",
"list",
"set",
"None", # <--- There is your oparg 7!
'""',
"True",
"False",
"-1",
"frozenset",
"()",
AssertionError,
NotImplementedError,
tuple,
all,
any,
list,
set,
None,
"",
True,
False,
-1,
frozenset,
(),
)


def format_LOAD_COMMON_CONSTANT_315(arg: int):
obj = _common_constants[arg]
return obj.__name__ if isinstance(obj, type) else repr(obj)


def resolve_LOAD_COMMON_CONSTANT_315(arg: int):
return _common_constants[arg]


Expand All @@ -400,6 +405,10 @@ def format_LOAD_COMMON_CONSTANT_315(arg: int):
**{"LOAD_COMMON_CONSTANT": format_LOAD_COMMON_CONSTANT_315},
}

opcode_arg_val = opcode_arg_val315 = {
"LOAD_COMMON_CONSTANT": resolve_LOAD_COMMON_CONSTANT_315,
}

opcode_extended_fmt = opcode_extended_fmt315 = {
**opcode_314.opcode_extended_fmt314,
**{"BINARY_OP": extended_BINARY_OP_315},
Expand Down