Skip to content

Commit bb6f038

Browse files
Merge pull request #133 from MatthieuDartiailh/forbid-pseudo-opcode
Forbid pseudo opcode
2 parents f2c045b + 1052f64 commit bb6f038

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

doc/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ChangeLog
66

77
Bugfixes:
88

9+
- Disallow creating an instruction targeting a pseudo/instrumented opcode PR #133
910
- Fixes encoding of 0 as a varint PR #132
1011
- Correct spelling of "INTRINSIC" in several places; this affected
1112
some ops in Python 3.12. PR #131

src/bytecode/instr.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
import bytecode as _bytecode
1616

17-
# --- Instruction argument tools and abstractions
17+
# --- Instruction argument tools and
18+
19+
MIN_INSTRUMENTED_OPCODE = getattr(_opcode, "MIN_INSTRUMENTED_OPCODE", 256)
1820

1921
# Instructions relying on a bit to modify its behavior.
2022
# The lowest bit is used to encode custom behavior.
@@ -734,6 +736,12 @@ def _set(self, name: str, arg: A) -> None:
734736
except KeyError:
735737
raise ValueError(f"invalid operation name: {name}")
736738

739+
if opcode >= MIN_INSTRUMENTED_OPCODE:
740+
raise ValueError(
741+
f"operation {name} is an instrumented or pseudo opcode. "
742+
"Only base opcodes are supported"
743+
)
744+
737745
self._check_arg(name, opcode, arg)
738746

739747
self._name = name

tests/test_instr.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ def test_repr(self):
138138
self.assertIn("arg", r)
139139
self.assertIn("_x_", r)
140140

141+
def test_reject_pseudo_opcode(self):
142+
if sys.version_info >= (3, 12):
143+
with self.assertRaises(ValueError) as e:
144+
Instr("LOAD_METHOD", "x")
145+
self.assertIn("is an instrumented or pseudo opcode", str(e.exception))
146+
141147
def test_invalid_arg(self):
142148
label = Label()
143149
block = BasicBlock()

0 commit comments

Comments
 (0)