Skip to content

Commit eb56114

Browse files
authored
Take a borrow in invoke_exception instead of an owned type (#8327)
1 parent a7f0496 commit eb56114

10 files changed

Lines changed: 17 additions & 23 deletions

File tree

‎crates/capi/src/pyerrors.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ pub unsafe extern "C" fn PyErr_SetString(exception: *mut PyObject, message: *con
147147
let exc_type = unsafe { &*exception }.try_downcast_ref::<PyType>(vm)?;
148148
let message = unsafe { message.try_as_str(vm) }?;
149149

150-
let exc = vm.invoke_exception(
151-
exc_type.to_owned(),
152-
vec![vm.ctx.new_str(message).into_object()],
153-
)?;
150+
let exc = vm.invoke_exception(exc_type, vec![vm.ctx.new_str(message).into_object()])?;
154151

155152
Err(exc)
156153
})

‎crates/vm/src/exception_group.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,8 @@ pub(super) mod types {
7171
vm: &VirtualMachine,
7272
) -> PyResult {
7373
let message = zelf.get_arg(0).unwrap_or_else(|| vm.ctx.new_str("").into());
74-
vm.invoke_exception(
75-
vm.ctx.exceptions.base_exception_group.to_owned(),
76-
vec![message, excs],
77-
)
78-
.map(|e| e.into())
74+
vm.invoke_exception(vm.ctx.exceptions.base_exception_group, vec![message, excs])
75+
.map(|e| e.into())
7976
}
8077

8178
#[pymethod]

‎crates/vm/src/exceptions.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ impl VirtualMachine {
353353

354354
pub fn invoke_exception(
355355
&self,
356-
cls: PyTypeRef,
356+
cls: &Py<PyType>,
357357
args: Vec<PyObjectRef>,
358358
) -> PyResult<PyBaseExceptionRef> {
359359
// TODO: fast-path built-in exceptions by directly instantiating them? Is that really worth it?
360-
let res = PyType::call(&cls, args.into_args(self), self)?;
360+
let res = PyType::call(cls, args.into_args(self), self)?;
361361
res.downcast::<PyBaseException>().map_err(|obj| {
362362
self.new_type_error(format!(
363363
"calling {} should have returned an instance of BaseException, not {}",
@@ -444,7 +444,7 @@ impl TryFromObject for ExceptionCtor {
444444
impl ExceptionCtor {
445445
pub fn instantiate(self, vm: &VirtualMachine) -> PyResult<PyBaseExceptionRef> {
446446
match self {
447-
Self::Class(cls) => vm.invoke_exception(cls, vec![]),
447+
Self::Class(cls) => vm.invoke_exception(&cls, vec![]),
448448
Self::Instance(exc) => Ok(exc),
449449
}
450450
}
@@ -472,7 +472,7 @@ impl ExceptionCtor {
472472
exc @ PyBaseException => exc.args().to_vec(),
473473
obj => vec![obj],
474474
});
475-
vm.invoke_exception(cls, args)
475+
vm.invoke_exception(&cls, args)
476476
}
477477
}
478478
}
@@ -2101,7 +2101,7 @@ pub(super) mod types {
21012101
.downcast_ref::<PyInt>()
21022102
.and_then(|errno| errno.try_to_primitive::<i32>(vm).ok())
21032103
.and_then(|errno| super::errno_to_exc_type(errno, vm))
2104-
.and_then(|typ| vm.invoke_exception(typ.to_owned(), args_vec).ok())
2104+
.and_then(|typ| vm.invoke_exception(typ, args_vec).ok())
21052105
{
21062106
return error.to_pyresult(vm);
21072107
}

‎crates/vm/src/stdlib/_ctypes/function.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ fn check_hresult(hresult: i32, zelf: &Py<PyCFuncPtr>, vm: &VirtualMachine) -> Py
13611361
.new_str(format!("HRESULT: 0x{:08X}", hresult as u32))
13621362
.into();
13631363
let details: PyObjectRef = vm.ctx.none();
1364-
let exc = vm.invoke_exception(com_error_type, vec![text.clone(), details.clone()])?;
1364+
let exc = vm.invoke_exception(&com_error_type, vec![text.clone(), details.clone()])?;
13651365
let _ = exc.as_object().set_attr("hresult", hresult_obj, vm);
13661366
let _ = exc.as_object().set_attr("text", text, vm);
13671367
let _ = exc.as_object().set_attr("details", details, vm);

‎crates/vm/src/stdlib/_io.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ mod _io {
943943
None => {
944944
// BlockingIOError(errno, msg, characters_written=0)
945945
return Err(vm.invoke_exception(
946-
vm.ctx.exceptions.blocking_io_error.to_owned(),
946+
vm.ctx.exceptions.blocking_io_error,
947947
vec![
948948
vm.new_pyobj(EAGAIN),
949949
vm.new_pyobj("write could not complete without blocking"),
@@ -1154,7 +1154,7 @@ mod _io {
11541154
self.write_end += avail as Offset;
11551155
self.pos += avail as Offset;
11561156
return Err(vm.invoke_exception(
1157-
vm.ctx.exceptions.blocking_io_error.to_owned(),
1157+
vm.ctx.exceptions.blocking_io_error,
11581158
vec![
11591159
vm.new_pyobj(EAGAIN),
11601160
vm.new_pyobj("write could not complete without blocking"),
@@ -1200,7 +1200,7 @@ mod _io {
12001200
// BlockingIOError(errno, msg, characters_written)
12011201
let chars_written = written + buffer_len;
12021202
return Err(vm.invoke_exception(
1203-
vm.ctx.exceptions.blocking_io_error.to_owned(),
1203+
vm.ctx.exceptions.blocking_io_error,
12041204
vec![
12051205
vm.new_pyobj(EAGAIN),
12061206
vm.new_pyobj("write could not complete without blocking"),

‎crates/vm/src/stdlib/_thread.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ pub(crate) mod _thread {
623623

624624
#[pyfunction]
625625
fn exit(vm: &VirtualMachine) -> PyResult {
626-
Err(vm.invoke_exception(vm.ctx.exceptions.system_exit.to_owned(), vec![])?)
626+
Err(vm.invoke_exception(vm.ctx.exceptions.system_exit, vec![])?)
627627
}
628628

629629
thread_local!(static SENTINELS: RefCell<Vec<PyRef<Lock>>> = const { RefCell::new(Vec::new()) });

‎crates/vm/src/stdlib/builtins.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ mod builtins {
10411041
#[pyfunction]
10421042
pub(super) fn exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
10431043
let code = exit_code_arg.unwrap_or_else(|| vm.ctx.new_int(0).into());
1044-
Err(vm.invoke_exception(vm.ctx.exceptions.system_exit.to_owned(), vec![code])?)
1044+
Err(vm.invoke_exception(vm.ctx.exceptions.system_exit, vec![code])?)
10451045
}
10461046

10471047
#[derive(Debug, Default, FromArgs)]

‎crates/vm/src/stdlib/sys.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ pub mod sys {
776776
} else {
777777
vec![status]
778778
};
779-
let exc = vm.invoke_exception(vm.ctx.exceptions.system_exit.to_owned(), args)?;
779+
let exc = vm.invoke_exception(vm.ctx.exceptions.system_exit, args)?;
780780
Err(exc)
781781
}
782782

‎crates/vm/src/vm/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@ impl VirtualMachine {
21612161
if self.state.finalizing.load(Ordering::Acquire) && !self.is_main_thread() {
21622162
// once finalization starts,
21632163
// non-main Python threads should stop running bytecode.
2164-
return Err(self.invoke_exception(self.ctx.exceptions.system_exit.to_owned(), vec![])?);
2164+
return Err(self.invoke_exception(self.ctx.exceptions.system_exit, vec![])?);
21652165
}
21662166

21672167
// Suspend this thread if stop-the-world is in progress

‎crates/vm/src/vm/vm_new.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ impl VirtualMachine {
894894
}
895895

896896
pub fn new_stop_iteration(&self, value: Option<PyObjectRef>) -> PyBaseExceptionRef {
897-
let stop_iteration_error = self.ctx.exceptions.stop_iteration.to_owned();
897+
let stop_iteration_error = self.ctx.exceptions.stop_iteration;
898898
let args = if let Some(value) = value {
899899
vec![value]
900900
} else {

0 commit comments

Comments
 (0)