Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

161 questions
3
votes
1 answer

How to pass a Rust function as a callback to Python using pyo3

I am using Pyo3 to call Rust functions from Python and vice versa. I am trying to achieve the following: Python calls rust_function_1 Rust function rust_function_1 calls Python function python_function passing Rust function rust_function_2 as a…
Bruno Rijsman
  • 3,715
  • 4
  • 31
  • 61
3
votes
3 answers

How to configure os specific dependencies in a pyproject.toml file [Maturin]

I have a rust and python project that I am building using Maturin(https://github.com/PyO3/maturin). It says that it requires a pyproject.toml file for the python dependencies. I have a dependency of uvloop, which is not supported on windows and arm…
Sanskar Jethi
  • 544
  • 5
  • 17
3
votes
0 answers

How to capture the stdout of the Python interpreter inside the Rust program when using the pyo3 crate

I am using the Rust bindings of the Python interpreter provided by the crate pyo3. I have the following code: fn run(script: &str) { Python::with_gil(|py| py.run(script, None, None)).unwrap(); } fn main() { run("print('abc')"); } I would…
3
votes
1 answer

PyO3 implement python iterable class in Rust

I've found example of how to implement PyIterProtocol in Rust. use pyo3::prelude::*; use pyo3::PyIterProtocol; use pyo3::class::iter::IterNextOutput; #[pyclass] struct Iter { count: usize } #[pyproto] impl PyIterProtocol for Iter { fn…
alagris
  • 1,838
  • 16
  • 31
3
votes
1 answer

PyO3 convert rust struct to &PyAny

I have a struct #[pyclass] pub struct DynMat { ... } and I have this function #[pyfunction] #[text_signature = "(tensor/)"] pub fn exp<'py>(py: Python<'py>, tensor_or_scalar: &'py PyAny) -> PyResult<&'py PyAny> { // I need to return &PyAny…
alagris
  • 1,838
  • 16
  • 31
3
votes
1 answer

Converting PyAny to a PyRef of a given type, without the extra cost of extract?

Minimal example that works but isn't quite what I want: use std::collections::HashMap; use pyo3::class::basic::CompareOp; use pyo3::class::PyObjectProtocol; use pyo3::prelude::*; #[pyclass] struct MyStruct { foo: HashMap
cadolphs
  • 9,014
  • 1
  • 24
  • 41
3
votes
2 answers

Why cython embeded plugins has higher performance in cpython interpreter than rust-c interface versions?

I would like to ask some questions about the underlying principles of python interpreters, because I didn't get much useful information during my own search. I've been using rust to write python plugins lately, this gives a significant speedup to…
AdamHommer
  • 657
  • 7
  • 22
3
votes
2 answers

Raising an exception with pyo3

How do I correctly raise an Exception? I've tried the following: #[pymethods] impl Foo { #[new] fn __new__(arg1: u8, ...) -> Self { if arg1 > LIMIT { let gil = Python::acquire_gil(); let py = gil.python(); …
Martin Bammer
  • 537
  • 7
  • 19
3
votes
1 answer

Passing Python Object Into Rust

I am trying to pass a Python object into rust and perform operations using the fields of the Python object. Python: class myclass(object): def __init__(self): self.a = 3 b = myclass() print(b.a) // 3 Rust: #[pyfn(m, "rust_obj")] fn…
grayfox57
  • 31
  • 1
2
votes
0 answers

Forward `SIGINT` to Rust code when using Pyo3 without having handle this in all Rust code

I am building a Python package which is mostly implemented in Rust, with bindings provided by Pyo3. Some of the Rust calls can take a long time, and I would like to be able to interrupt the process with Ctrl-c while these calls are happening. The…
AlbertGarde
  • 369
  • 1
  • 13
2
votes
1 answer

How to extract text from Whisper transcribe python API via Rust PyO3

I am attempting to use the python whisper speech to text API via PyO3 rust code. For the example given on the whisper github found here import whisper model = whisper.load_model("base") result =…
Zadeis
  • 23
  • 4
2
votes
0 answers

Polars conversion error going from Python to Rust with pyo3

Related to the solution proposed in this other question. This code creates a polars dataframe of python dictionaries. In Python land, everything is fine. The code fails when we extract to Rust land. use pyo3::prelude::*; use pyo3_polars::*; let…
GrantS
  • 781
  • 7
  • 16
2
votes
1 answer

Closest implementation for enums with fields in pyO3

As of PyO3 version 0.18.1, only fieldless enums are supported. What are the potential implementations to support enums with fields using PyO3 if implemented manually?? pyclass is of no use here (Correct me if I am wrong). For instance if I have to…
iamsmkr
  • 800
  • 2
  • 10
  • 29
2
votes
1 answer

Exporting HashMap of HashMap to Python

I have a text parser written in Rust and want to provide a Python interface to it using pyo3. The parser returns a HashMap within a HashMap and the values of the inner HashMap are of type serde_json::Value. When I try to return this as a PyObject I…
leviathan
  • 363
  • 3
  • 10
2
votes
1 answer

A copied socket is not being pickled

I am trying to copy a socket and send it to a different process in Python. The socket is created in rust and is shared as a Python object through PyO3. Here is the shared socket code use pyo3::prelude::*; use socket2::{Domain, Protocol, Socket,…
Sanskar Jethi
  • 544
  • 5
  • 17
1 2
3
10 11