Skip to content

Commit 3514126

Browse files
committed
vm.enter_and_expect
1 parent 6adb727 commit 3514126

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

‎vm/src/vm/interpreter.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ impl Interpreter {
7676
thread::enter_vm(&self.vm, || f(&self.vm))
7777
}
7878

79+
/// Run [`enter`] and call `expect_pyresult` for the result.
80+
///
81+
/// This function is useful when you want to expect a result from the function,
82+
/// but also print useful panic information when exception raised.
83+
///
84+
/// See [`enter`] for more information.
85+
/// See [`expect_pyresult`] for more information.
86+
pub fn enter_and_expect<F, R>(&self, f: F, msg: &str) -> R
87+
where
88+
F: FnOnce(&VirtualMachine) -> PyResult<R>,
89+
{
90+
self.enter(|vm| {
91+
let result = f(vm);
92+
vm.expect_pyresult(result, msg)
93+
})
94+
}
95+
7996
/// Run a function with the main virtual machine and return exit code.
8097
///
8198
/// To enter vm context only once and safely terminate the vm, this function is preferred.

0 commit comments

Comments
 (0)