Skip to content

gh-100077: make test_code.test_invalid_bytecode more robust and maintainable #100078

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

Merged
merged 1 commit into from
Dec 7, 2022
Merged
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
20 changes: 12 additions & 8 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
gc_collect)
from test.support.script_helper import assert_python_ok
from test.support import threading_helper
from opcode import opmap
from opcode import opmap, opname
COPY_FREE_VARS = opmap['COPY_FREE_VARS']


Expand Down Expand Up @@ -339,15 +339,19 @@ def func():
self.assertEqual(list(new_code.co_lines()), [])

def test_invalid_bytecode(self):
def foo(): pass
foo.__code__ = co = foo.__code__.replace(co_code=b'\xee\x00d\x00S\x00')
def foo():
pass

with self.assertRaises(SystemError) as se:
foo()
self.assertEqual(
f"{co.co_filename}:{co.co_firstlineno}: unknown opcode 238",
str(se.exception))
# assert that opcode 238 is invalid
self.assertEqual(opname[238], '<238>')

# change first opcode to 0xee (=238)
foo.__code__ = foo.__code__.replace(
co_code=b'\xee' + foo.__code__.co_code[1:])

msg = f"unknown opcode 238"
with self.assertRaisesRegex(SystemError, msg):
foo()

@requires_debug_ranges()
def test_co_positions_artificial_instructions(self):
Expand Down