Skip to content

Commit 75013eb

Browse files
fix binary op arg coercion
1 parent 8a7a4f7 commit 75013eb

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/bytecode/instr.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ def _cmp_key(self) -> Tuple[Optional[InstrLocation], str, Any]:
890890
arg = const_key(arg)
891891
return (self._location, self._name, arg)
892892

893-
def _check_arg(self, name: str, opcode: int, arg: InstrArg) -> None:
893+
def _check_arg(self, name: str, opcode: int, arg: InstrArg) -> None: # noqa: C901
894894
if name == "EXTENDED_ARG":
895895
raise ValueError(
896896
"only concrete instruction can contain EXTENDED_ARG, "
@@ -993,10 +993,11 @@ def _check_arg(self, name: str, opcode: int, arg: InstrArg) -> None:
993993
"operation %s argument type must be "
994994
"coercible to BinaryOp, got %s" % (name, type(arg).__name__)
995995
) from e
996-
raise TypeError(
997-
"operation %s argument type must be "
998-
"BinaryOp, got %s" % (name, type(arg).__name__)
999-
)
996+
else:
997+
raise TypeError(
998+
"operation %s argument type must be "
999+
"BinaryOp, got %s" % (name, type(arg).__name__)
1000+
)
10001001

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

tests/test_instr.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ def test_invalid_arg(self):
165165

166166
for name in (opcode.opname[op] for op in BINARY_OPS):
167167
assert name == "BINARY_OP", f"expected BINARY_OP but got {name=}"
168-
if PY314:
169-
Instr(name, BinaryOp.SUBSCR)
170-
Instr(name, BinaryOp.SUBSCR.value)
171-
self.assertRaises(TypeError, Instr, name, "")
168+
Instr(name, BinaryOp.ADD)
169+
Instr(name, BinaryOp.ADD.value)
170+
self.assertRaises(TypeError, Instr, name, "")
172171

173172
for name in (opcode.opname[op] for op in SPECIAL_OPS):
174173
assert name == "LOAD_SPECIAL", f"expected LOAD_SPECIAL but got {name=}"
@@ -186,7 +185,7 @@ def test_invalid_arg(self):
186185
TypeError,
187186
Instr,
188187
"LOAD_COMMON_CONSTANT",
189-
CommonConstants.BUILTIN_ALL.value,
188+
CommonConstant.BUILTIN_ALL.value,
190189
)
191190

192191
for name in (opcode.opname[op] for op in SMALL_INT_OPS):

0 commit comments

Comments
 (0)