]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-96711: Enhance SystemError message upon Invalid opcode (#96712)
authorserge-sans-paille <serge.guelton@telecom-bretagne.eu>
Tue, 20 Sep 2022 10:00:34 +0000 (10:00 +0000)
committerGitHub <noreply@github.com>
Tue, 20 Sep 2022 10:00:34 +0000 (12:00 +0200)
Raise verbose SystemError instead of printing debug information
upon Invalid opcode.

Fix #96711

Lib/test/test_code.py
Python/ceval.c

index 2386cf6b59f396a06e5a922a47a29cd78b7f9a06..2fdfdd0d309c5d3075b8e1b89dab25ed10ad02ba 100644 (file)
@@ -337,6 +337,17 @@ class CodeTest(unittest.TestCase):
         new_code = code = func.__code__.replace(co_linetable=b'')
         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')
+
+        with self.assertRaises(SystemError) as se:
+            foo()
+        self.assertEqual(
+            f"{co.co_filename}:{co.co_firstlineno}: unknown opcode 238",
+            str(se.exception))
+
+
     @requires_debug_ranges()
     def test_co_positions_artificial_instructions(self):
         import dis
index a07fb4964b48f05dbdf8dc4bba644ec0a9700a51..83c1e1cbeeaf32950128159a62b095ee7f3fb1ac 100644 (file)
@@ -5051,9 +5051,11 @@ handle_eval_breaker:
             /* Tell C compilers not to hold the opcode variable in the loop.
                next_instr points the current instruction without TARGET(). */
             opcode = _Py_OPCODE(*next_instr);
-            fprintf(stderr, "XXX lineno: %d, opcode: %d\n",
-                    _PyInterpreterFrame_GetLine(frame),  opcode);
-            _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
+            _PyErr_Format(tstate, PyExc_SystemError,
+                          "%U:%d: unknown opcode %d",
+                          frame->f_code->co_filename,
+                          _PyInterpreterFrame_GetLine(frame),
+                          opcode);
             goto error;
 
         } /* End instructions */