【发布时间】:2020-07-28 06:12:35
【问题描述】:
如何正确引发异常?
我尝试了以下方法:
#[pymethods]
impl Foo {
#[new]
fn __new__(arg1: u8, ...) -> Self {
if arg1 > LIMIT {
let gil = Python::acquire_gil();
let py = gil.python();
PyErr::new::<exceptions::ValueError, _>("Parameter arg1 has invalid value").restore(py);
}
Foo {...}
}
这与here 的描述完全相同。
当我使用无效参数值创建Foo 的实例时,会引发SystemError 而不是SystemError,并带有错误文本<class 'Foo'> returned a result with an error set。
我在 Linux 上每晚使用 pyo3 0.11.1 和 Rust 1.47.0。
【问题讨论】: