Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid pseudo opcode #133

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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