Add new_payload_exception helper for constructing built-in payload exceptions by kangdora · Pull Request #8403 · RustPython/RustPython · GitHub
Skip to content
15 changes: 6 additions & 9 deletions crates/vm/src/exceptions.rs
60 changes: 35 additions & 25 deletions crates/vm/src/stdlib/_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ cfg_select! {
}

use crate::{
AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, builtins::PyModule,
AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
builtins::{PyModule, PyOSError},
};
pub use _io::{OpenArgs, io_open as open};
use rustpython_host_env::io as host_io;
Expand Down Expand Up @@ -943,14 +944,17 @@ mod _io {
Some(n) => n,
None => {
// BlockingIOError(errno, msg, characters_written=0)
return Err(vm.invoke_exception(
vm.ctx.exceptions.blocking_io_error,
vec![
vm.new_pyobj(EAGAIN),
vm.new_pyobj("write could not complete without blocking"),
vm.new_pyobj(0),
],
)?);
return Err(vm
.new_payload_exception::<PyOSError>(
vm.ctx.exceptions.blocking_io_error.to_owned(),
vec![
vm.new_pyobj(EAGAIN),
vm.new_pyobj("write could not complete without blocking"),
vm.new_pyobj(0),
]
.into(),
)?
.upcast());
}
};
self.write_pos += n as Offset;
Expand Down Expand Up @@ -1154,14 +1158,17 @@ mod _io {
self.buffer[self.write_end as usize..][..avail].copy_from_slice(&buf[..avail]);
self.write_end += avail as Offset;
self.pos += avail as Offset;
return Err(vm.invoke_exception(
vm.ctx.exceptions.blocking_io_error,
vec![
vm.new_pyobj(EAGAIN),
vm.new_pyobj("write could not complete without blocking"),
vm.new_pyobj(avail),
],
)?);
return Err(vm
.new_payload_exception::<PyOSError>(
vm.ctx.exceptions.blocking_io_error.to_owned(),
vec![
vm.new_pyobj(EAGAIN),
vm.new_pyobj("write could not complete without blocking"),
vm.new_pyobj(avail),
]
.into(),
)?
.upcast());
}
Err(e) => return Err(e),
}
Expand Down Expand Up @@ -1200,14 +1207,17 @@ mod _io {
self.write_end = buffer_size;
// BlockingIOError(errno, msg, characters_written)
let chars_written = written + buffer_len;
return Err(vm.invoke_exception(
vm.ctx.exceptions.blocking_io_error,
vec![
vm.new_pyobj(EAGAIN),
vm.new_pyobj("write could not complete without blocking"),
vm.new_pyobj(chars_written),
],
)?);
return Err(vm
.new_payload_exception::<PyOSError>(
vm.ctx.exceptions.blocking_io_error.to_owned(),
vec![
vm.new_pyobj(EAGAIN),
vm.new_pyobj("write could not complete without blocking"),
vm.new_pyobj(chars_written),
]
.into(),
)?
.upcast());
}
None => break,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ pub(crate) mod _thread {

#[pyfunction]
fn exit(vm: &VirtualMachine) -> PyResult {
Err(vm.invoke_exception(vm.ctx.exceptions.system_exit, vec![])?)
Err(vm.new_system_exit(vec![].into()))
}

thread_local!(static SENTINELS: RefCell<Vec<PyRef<Lock>>> = const { RefCell::new(Vec::new()) });
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ mod builtins {
#[pyfunction]
pub(super) fn exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
let code = exit_code_arg.unwrap_or_else(|| vm.ctx.new_int(0).into());
Err(vm.invoke_exception(vm.ctx.exceptions.system_exit, vec![code])?)
Err(vm.new_system_exit(vec![code].into()))
}

#[derive(Debug, Default, FromArgs)]
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,7 @@ pub mod sys {
} else {
vec![status]
};
let exc = vm.invoke_exception(vm.ctx.exceptions.system_exit, args)?;
Err(exc)
Err(vm.new_system_exit(args.into()))
}

#[pyfunction]
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2167,7 +2167,7 @@ impl VirtualMachine {
if self.state.finalizing.load(Ordering::Acquire) && !self.is_main_thread() {
// once finalization starts,
// non-main Python threads should stop running bytecode.
return Err(self.invoke_exception(self.ctx.exceptions.system_exit, vec![])?);
return Err(self.new_system_exit(vec![].into()));
}

// Suspend this thread if stop-the-world is in progress
Expand Down
49 changes: 37 additions & 12 deletions crates/vm/src/vm/vm_new.rs
Loading