Skip to content

Commit fc05107

Browse files
gh-96711: Enhance SystemError message upon Invalid opcode (#96712)
Raise verbose SystemError instead of printing debug information upon Invalid opcode. Fix #96711
1 parent 6ad47b4 commit fc05107

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Lib/test/test_code.py

+11
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ def func():
337337
new_code = code = func.__code__.replace(co_linetable=b'')
338338
self.assertEqual(list(new_code.co_lines()), [])
339339

340+
def test_invalid_bytecode(self):
341+
def foo(): pass
342+
foo.__code__ = co = foo.__code__.replace(co_code=b'\xee\x00d\x00S\x00')
343+
344+
with self.assertRaises(SystemError) as se:
345+
foo()
346+
self.assertEqual(
347+
f"{co.co_filename}:{co.co_firstlineno}: unknown opcode 238",
348+
str(se.exception))
349+
350+
340351
@requires_debug_ranges()
341352
def test_co_positions_artificial_instructions(self):
342353
import dis

Python/ceval.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -5051,9 +5051,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
50515051
/* Tell C compilers not to hold the opcode variable in the loop.
50525052
next_instr points the current instruction without TARGET(). */
50535053
opcode = _Py_OPCODE(*next_instr);
5054-
fprintf(stderr, "XXX lineno: %d, opcode: %d\n",
5055-
_PyInterpreterFrame_GetLine(frame), opcode);
5056-
_PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
5054+
_PyErr_Format(tstate, PyExc_SystemError,
5055+
"%U:%d: unknown opcode %d",
5056+
frame->f_code->co_filename,
5057+
_PyInterpreterFrame_GetLine(frame),
5058+
opcode);
50575059
goto error;
50585060

50595061
} /* End instructions */

0 commit comments

Comments
 (0)