Skip to content

Commit 9d187ca

Browse files
instr: allow to coerce int to FormatValue for backward compatibility
1 parent 77de151 commit 9d187ca

3 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/bytecode/instr.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,18 @@ 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-
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
988+
if isinstance(arg, int):
989+
try:
990+
arg = BinaryOp(arg)
991+
except Exception as e:
992+
raise TypeError(
993+
"operation %s argument type must be "
994+
"coercible to BinaryOp, got %s" % (name, type(arg).__name__)
995+
) from e
996+
raise TypeError(
997+
"operation %s argument type must be "
998+
"BinaryOp, got %s" % (name, type(arg).__name__)
999+
)
9951000

9961001
# We do not enforce constant immortality since which constants are
9971002
# immortal may differ between recompilation and execution.
@@ -1048,10 +1053,19 @@ def _check_arg(self, name: str, opcode: int, arg: InstrArg) -> None:
10481053
"got %s (value=%s)" % (name, type(arg).__name__, str(arg))
10491054
)
10501055
elif not isinstance(arg, FormatValue):
1051-
raise TypeError(
1052-
"operation %s argument must be a FormatValue] "
1053-
"got %s (value=%s)" % (name, type(arg).__name__, str(arg))
1054-
)
1056+
if isinstance(arg, int):
1057+
try:
1058+
arg = FormatValue(arg)
1059+
except Exception as e:
1060+
raise TypeError(
1061+
"operation %s argument must be a FormatValue] "
1062+
"got %s (value=%s)" % (name, type(arg).__name__, str(arg))
1063+
) from e
1064+
else:
1065+
raise TypeError(
1066+
"operation %s argument must be a FormatValue] "
1067+
"got %s (value=%s)" % (name, type(arg).__name__, str(arg))
1068+
)
10551069

10561070
elif opcode_has_argument(opcode):
10571071
_check_arg_int(arg, name)

tests/test_bytecode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def func(a):
380380
if PY314
381381
else Instr("LOAD_FAST", "a", lineno=2),
382382
Instr("CONVERT_VALUE", FormatValue.REPR, lineno=2),
383+
Instr("FORMAT_SIMPLE", lineno=2),
383384
Instr("RETURN_VALUE", lineno=2),
384385
]
385386
if PY313

tests/test_instr.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,15 @@ def test_invalid_arg(self):
195195
self.assertRaises(ValueError, Instr, "LOAD_SMALL_INT", 256)
196196

197197
for name in (opcode.opname[op] for op in FORMAT_VALUE_OPS):
198-
if name == "CONVERT_VALUE":
199-
Instr(name, FormatValue.STR)
200-
self.assertRaises(TypeError, Instr, name, FormatValue.STR.value)
201-
Instr(name, FormatValue.STR)
202-
self.assertRaises(TypeError, Instr, name, FormatValue.STR.value)
203-
elif name == "BUILD_INTERPOLATION":
198+
if name in BITFLAG_OPCODES:
204199
Instr(name, (True, FormatValue.STR))
205200
Instr(name, (False, FormatValue.STR))
206201
self.assertRaises(TypeError, Instr, name, True, FormatValue.STR)
207202
self.assertRaises(TypeError, Instr, name, False, FormatValue.STR)
208203
else:
209-
raise ValueError(
210-
f"expected CONVERT_VALUE or BUILD_INTERPOLATION but got {name=}"
211-
)
204+
Instr(name, FormatValue.STR)
205+
Instr(name, FormatValue.STR.value)
206+
self.assertRaises(TypeError, Instr, name, "STR")
212207

213208
# EXTENDED_ARG
214209
self.assertRaises(ValueError, Instr, "EXTENDED_ARG", 0)

0 commit comments

Comments
 (0)