Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make SystemExit.code a writable attribute set at init
  • Loading branch information
kangdora committed Jul 13, 2026
commit 7ed3e0415d923d1ad8d198b3561afa79fa0adb00
26 changes: 9 additions & 17 deletions crates/vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ impl ExceptionZoo {
});

extend_exception!(PySystemExit, ctx, excs.system_exit, {
"code" => ctx.new_readonly_getset("code", excs.system_exit, system_exit_code),
"code" => ctx.none(),
});
extend_exception!(PyKeyboardInterrupt, ctx, excs.keyboard_interrupt);
extend_exception!(PyGeneratorExit, ctx, excs.generator_exit);
Expand Down Expand Up @@ -1100,19 +1100,6 @@ fn syntax_error_set_msg(exc: PyBaseExceptionRef, value: PySetterValue, vm: &Virt
*args = PyTuple::new_ref(new_args, &vm.ctx);
}

fn system_exit_code(exc: PyBaseExceptionRef) -> Option<PyObjectRef> {
// SystemExit.code based on args length:
// - size == 0: code is None
// - size == 1: code is args[0]
// - size > 1: code is args (the whole tuple)
let args = exc.args.read();
Some(match args.len() {
0 => return None,
1 => args.first().unwrap().clone(),
_ => args.as_object().to_owned(),
})
}

#[cfg(feature = "serde")]
pub struct SerializeException<'vm, 's> {
vm: &'vm VirtualMachine,
Expand Down Expand Up @@ -1637,9 +1624,14 @@ pub(super) mod types {
type Args = FuncArgs;
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
// Call BaseException_init first (handles args)
PyBaseException::slot_init(zelf, args, vm)
// Note: code is computed dynamically via system_exit_code getter
// so we don't need to set it here explicitly
let code = match args.args.len() {
0 => vm.ctx.none(),
1 => args.args[0].clone(),
_ => vm.ctx.new_tuple(args.args.clone()).into(),
};
PyBaseException::slot_init(zelf.clone(), args, vm)?;
zelf.set_attr("code", code, vm)?;
Ok(())
}

fn init(_zelf: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
Expand Down