Skip to content

Commit f3e97c9

Browse files
authored
gh-100077: make test_code.test_invalid_bytecode more robust and maintainable (#100078)
1 parent 68e4129 commit f3e97c9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Lib/test/test_code.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
gc_collect)
144144
from test.support.script_helper import assert_python_ok
145145
from test.support import threading_helper
146-
from opcode import opmap
146+
from opcode import opmap, opname
147147
COPY_FREE_VARS = opmap['COPY_FREE_VARS']
148148

149149

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

341341
def test_invalid_bytecode(self):
342-
def foo(): pass
343-
foo.__code__ = co = foo.__code__.replace(co_code=b'\xee\x00d\x00S\x00')
342+
def foo():
343+
pass
344344

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

348+
# change first opcode to 0xee (=238)
349+
foo.__code__ = foo.__code__.replace(
350+
co_code=b'\xee' + foo.__code__.co_code[1:])
351+
352+
msg = f"unknown opcode 238"
353+
with self.assertRaisesRegex(SystemError, msg):
354+
foo()
351355

352356
@requires_debug_ranges()
353357
def test_co_positions_artificial_instructions(self):

0 commit comments

Comments
 (0)