Pyo3:trait`PyClass`没有为`&Py<PyAny>`

2024-10-04 05:20:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习rust,并尝试使用pyo3maturin创建一个非常简单的python模块。但我对防锈代码有问题

Cargo.toml

[package]
name = "lenrs"
version = "0.1.0"
authors = ["matheusfillipe"]
edition = "2018"

[dependencies.pyo3]
version = "0.13.2"
features = ["extension-module"]

[lib]
crate-type = ["cdylib"]
name = "lenrs"

src/lib.rs

extern crate pyo3;

use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
fn length(py: Python, obj: &PyObject) -> PyResult<PyObject> {
    if let Ok(s) = obj.extract::<String>(py) {
        return Ok(s.len().to_object(py));
    }
    if let Ok(s) = obj.extract::<Vec<String>>(py) {
        return Ok(s.len().to_object(py));
    }
    Err(PyTypeError::new_err("Not Supported"))
}

#[pymodule]
fn lenrs(py: Python, m: &PyModule) -> PyResult<()> {
    m.add_wrapped(wrap_pyfunction!(length))?;
    Ok(())
}

lenrs/init.py

from .lenrs import *

构建输出

$ cargo build                 
   Compiling lenrs v0.1.0 (/home/matheus/projects/learn-rust/lenrs)
error[E0277]: the trait bound `&Py<PyAny>: PyClass` is not satisfied
 --> src/lib.rs:7:1
  |
7 | #[pyfunction]
  | ^^^^^^^^^^^^^ the trait `PyClass` is not implemented for `&Py<PyAny>`
  |
  = note: required because of the requirements on the impl of `pyo3::FromPyObject<'_>` for `&Py<PyAny>`
  = note: required because of the requirements on the impl of `ExtractExt<'_>` for `&Py<PyAny>`
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `lenrs`

To learn more, run the command again with --verbose.

我发现问题出在#[pyfunction]上。当我有一个只适用于字符串的更简单版本时,一切都很好,并且length函数将返回PyResult<()>,最后我只返回Ok(()),但现在我不确定如果不支持该类型,该如何让该函数引发python错误


Tags: ofthepyobjforuselibok
1条回答
网友
1楼 · 发布于 2024-10-04 05:20:27

我刚刚发现obj不应该是引用,将函数签名更改为:fn length(py: Python, obj: PyObject) -> PyResult<PyObject>使其工作:

extern crate pyo3;

use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
fn length(py: Python, obj: PyObject) -> PyResult<PyObject> {
    if let Ok(s) = obj.extract::<String>(py) {
        return Ok(s.len().to_object(py));
    }
    if let Ok(s) = obj.extract::<Vec<String>>(py) {
        return Ok(s.len().to_object(py));
    }
    Err(PyTypeError::new_err("Not Supported"))
}

#[pymodule]
fn lenrs(py: Python, m: &PyModule) -> PyResult<()> {
    m.add_wrapped(wrap_pyfunction!(length))?;
    Ok(())
}

相关问题 更多 >