#3020·brython

Raising an exception inside an exec on compiled code fails

Author: benjie-gitCreated Sep 23, 2026Updated Sep 23, 2026

Another exec on compiled code edge case. raise works inside an exec on a string, but not on compiled code. The compile() call runs, but the exec() crashes.

This also used to work ~5 months ago.

python
code = "print('Hi!'); raise RuntimeError('MyError')"
try:
    exec(code)
except RuntimeError as e:
    print(e)

astCode = compile(code, "<string>", "exec")
try:
    exec(astCode)
except RuntimeError as e:
    print(e)

prints:

Hi!
MyError

eval() error
 var $B = __BRYTHON__,
    _b_ = $B.builtins,
    locals = locals___main__,
    globals = globals___main___globals,
    locals___main__ = locals
var __file__ = locals.__file__ = "<string>"
locals.__name__ = "__main__"
locals.__doc__ = _b_.None
frame.positions = [[1,0,12],[1,20,23]]
locals.$annotations = {}
var stack_length = $B.count_frames()
try {
  $B.set_lineno(frame, 1);
  $B.$call_with_position(_b_.print, 1, 'Hi!')
  $B.set_lineno(frame, 1)
  $B.$raise($B.$call_with_position(_b_.RuntimeError, 3, 'MyError'), // unhandled class ast.undefined)
  $B.leave_frame({locals, value: _b_.None})
} catch (err) {
  $B.set_exc_and_trace(frame, err)
  $B.leave_frame({locals, value: _b_.None})
  throw err
}

error in loaders SyntaxError: missing ) after argument list (at VM12223:19:43)
    at new Function (<anonymous>)
    at r.eval (brython.js:1:209075)
    at r.exec (brython.js:1:209965)
    at e.$call (brython.js:1:86880)
    at e.$call_with_position (brython.js:1:86768)
    at eval (eval at e.loop (brython.js:1:69850), <anonymous>:56:8)
    at e.loop (brython.js:1:69886)
    at e.run_script (brython.js:1:549186)
    at e.loop (brython.js:1:70498)
    at n (brython.js:1:66351)