summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_code.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2022-12-07 18:09:05 (GMT)
committerGitHub <noreply@github.com>2022-12-07 18:09:05 (GMT)
commitf3e97c90ed6f82fce67b0e8757eec54908ba49ce (patch)
tree41318e0ad2942c586064f1b6463a2f0b2bba7672 /Lib/test/test_code.py
parent68e41295b8611a990de68f15c89f1eb3dea51867 (diff)
downloadcpython-f3e97c90ed6f82fce67b0e8757eec54908ba49ce.zip
cpython-f3e97c90ed6f82fce67b0e8757eec54908ba49ce.tar.gz
cpython-f3e97c90ed6f82fce67b0e8757eec54908ba49ce.tar.bz2
gh-100077: make test_code.test_invalid_bytecode more robust and maintainable (#100078)
Diffstat (limited to 'Lib/test/test_code.py')
-rw-r--r--Lib/test/test_code.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 4e4d823..02ab8fb 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -143,7 +143,7 @@ from test.support import (cpython_only,
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']
@@ -339,15 +339,19 @@ class CodeTest(unittest.TestCase):
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):