Skip to content

Commit 32e4640

Browse files
Raise verbose System error instead of printing debug information upon Invalid opcode
Also add code to test that path. Fix python#96711
1 parent 558768f commit 32e4640

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

Lib/test/test_code.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,37 @@ 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+
co = foo.__code__
343+
CodeType = type(co)
344+
345+
new_co = CodeType(
346+
co.co_argcount,
347+
co.co_posonlyargcount,
348+
co.co_kwonlyargcount,
349+
co.co_nlocals,
350+
co.co_stacksize,
351+
co.co_flags,
352+
b'\xee\x00d\x00S\x00',
353+
co.co_consts,
354+
co.co_names,
355+
co.co_varnames,
356+
co.co_filename,
357+
co.co_name,
358+
co.co_qualname,
359+
co.co_firstlineno,
360+
co.co_linetable,
361+
co.co_exceptiontable,
362+
co.co_freevars,
363+
co.co_cellvars)
364+
365+
foo.__code__ = new_co
366+
with self.assertRaises(SystemError) as se:
367+
foo()
368+
self.assertIn("unknown opcode '238'", str(se.exception))
369+
370+
340371
@requires_debug_ranges()
341372
def test_co_positions_artificial_instructions(self):
342373
import dis

Python/ceval.c

Lines changed: 5 additions & 3 deletions
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)