Skip to content

Commit

Permalink
pythongh-96711: Enhance SystemError message upon Invalid opcode (pyth…
Browse files Browse the repository at this point in the history
…on#96712)

Raise verbose SystemError instead of printing debug information
upon Invalid opcode.

Fix python#96711
  • Loading branch information
serge-sans-paille authored Sep 20, 2022
1 parent 6ad47b4 commit fc05107
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ def func():
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
Expand Down
8 changes: 5 additions & 3 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5051,9 +5051,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
/* 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 */
Expand Down

0 comments on commit fc05107

Please sign in to comment.