Skip to content

Commit cab273d

Browse files
instr: make usage of BinaryOp enum be backward compatible
1 parent b7a2345 commit cab273d

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/bytecode/instr.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,10 +985,13 @@ def _check_arg(self, name: str, opcode: int, arg: InstrArg) -> None:
985985

986986
elif opcode in BINARY_OPS:
987987
if not isinstance(arg, BinaryOp):
988-
raise TypeError(
989-
"operation %s argument type must be "
990-
"BinaryOp, got %s" % (name, type(arg).__name__)
991-
)
988+
try:
989+
arg = BinaryOp(arg)
990+
except Exception as e:
991+
raise TypeError(
992+
"operation %s argument type must be "
993+
"coercible to BinaryOp, got %s" % (name, type(arg).__name__)
994+
) from e
992995

993996
# We do not enforce constant immortality since which constants are
994997
# immortal may differ between recompilation and execution.

tests/test_instr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ def test_invalid_arg(self):
167167
assert name == "BINARY_OP", f"expected BINARY_OP but got {name=}"
168168
if PY314:
169169
Instr(name, BinaryOp.SUBSCR)
170-
self.assertRaises(TypeError, Instr, name, BinaryOp.SUBSCR.value)
170+
Instr(name, BinaryOp.SUBSCR.value)
171+
self.assertRaises(TypeError, Instr, name, "")
171172

172173
for name in (opcode.opname[op] for op in SPECIAL_OPS):
173174
assert name == "LOAD_SPECIAL", f"expected LOAD_SPECIAL but got {name=}"

0 commit comments

Comments
 (0)