Skip to content

Commit

Permalink
Merge pull request #133 from MatthieuDartiailh/forbid-pseudo-opcode
Browse files Browse the repository at this point in the history
Forbid pseudo opcode
  • Loading branch information
MatthieuDartiailh authored Oct 13, 2023
2 parents f2c045b + 1052f64 commit bb6f038
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ChangeLog

Bugfixes:

- Disallow creating an instruction targeting a pseudo/instrumented opcode PR #133
- Fixes encoding of 0 as a varint PR #132
- Correct spelling of "INTRINSIC" in several places; this affected
some ops in Python 3.12. PR #131
Expand Down
10 changes: 9 additions & 1 deletion src/bytecode/instr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import bytecode as _bytecode

# --- Instruction argument tools and abstractions
# --- Instruction argument tools and

MIN_INSTRUMENTED_OPCODE = getattr(_opcode, "MIN_INSTRUMENTED_OPCODE", 256)

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

if opcode >= MIN_INSTRUMENTED_OPCODE:
raise ValueError(
f"operation {name} is an instrumented or pseudo opcode. "
"Only base opcodes are supported"
)

self._check_arg(name, opcode, arg)

self._name = name
Expand Down
6 changes: 6 additions & 0 deletions tests/test_instr.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ def test_repr(self):
self.assertIn("arg", r)
self.assertIn("_x_", r)

def test_reject_pseudo_opcode(self):
if sys.version_info >= (3, 12):
with self.assertRaises(ValueError) as e:
Instr("LOAD_METHOD", "x")
self.assertIn("is an instrumented or pseudo opcode", str(e.exception))

def test_invalid_arg(self):
label = Label()
block = BasicBlock()
Expand Down

0 comments on commit bb6f038

Please sign in to comment.