Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

161 questions
5
votes
0 answers

Rust Pyo3 bindings: How to reuse python methods on Generic rust types

I've got a problem a bit more complicated than this. But I think this breaks it down. I have some generic struct, which has three constructors to get a concrete typed struct. They all have the same generic methods, except for the constructors. What…
ritchie46
  • 10,405
  • 1
  • 24
  • 43
5
votes
1 answer

How to implement trait FromPyObject for my rust struct

Consider a simple rust class exposed via pyo3 to python use pyo3::prelude::*; #[pyclass(name=MyClass)] pub struct PyMyClass { // Some fields } #[pymethods] impl PyMyStruct { #[new] fn py_new(obj: PyRawObject) { obj.init({ …
Matthias
  • 1,055
  • 2
  • 14
  • 26
4
votes
2 answers

Matching multiple possible types?

I'm very, very new to Rust and struggling with it because of my strong weakly typed programming background. The code below should write data being received from Python via PYO3 into a XLSX worksheet. I just don't know how to handle the last match,…
acortad
  • 101
  • 8
4
votes
0 answers

What’s the memory management strategy when using PyO3?

I am a beginner to Rust and PyO3, I read the PyO3 User Guide and got some information as shown below: When Rust calls Python, the memory of Python part is allocated in Python heap and is taken care by the Python garbage collector. When Python calls…
lulijun
  • 415
  • 3
  • 22
4
votes
0 answers

PyO3 can't find Rust compiler on M1, despite cargo / rustc / rustup installed and on path

I'm trying to get PyO3 working on my Mac M1 mini. If I follow the steps to get started with PyO3: $ mkdir string_sum $ cd string_sum $ python -m venv .env $ source .env/bin/activate $ pip install maturin I get "rustc...is not installed or not in…
twedl
  • 1,588
  • 1
  • 17
  • 28
4
votes
2 answers

Return reference to member field in PyO3

Suppose I have a Rust struct like this struct X{...} struct Y{ x:X } I'd like to be able to write python code that accesses X through Y y = Y() y.x.some_method() What would be the best way to implement it in PyO3? Currently I made two wrapper…
alagris
  • 1,838
  • 16
  • 31
4
votes
2 answers

How to create a heterogeneous Python dict in Rust with PyO3?

Based on this I can create a homogeneous Python dictionary. How can I create a dictionary with mixed-type values, e.g. make this work: let dict = [ ("num", 8), ("str", "asd") ].into_py_dict(py); ?
tungli
  • 351
  • 1
  • 9
4
votes
0 answers

How is memory managed with &str and String, native types and PyResult when passing values from Rust to Python?

Using PyO3, I am able to pass &str and String types from Rust to Python: #[pyfunction] fn test_str(py: Python) -> &str { "this is a &str" } #[pyfunction] fn test_string(py: Python) -> String { "this is a String".to_string() } And Python is…
user655321
  • 1,572
  • 2
  • 16
  • 33
4
votes
2 answers

Cross-compiling (unixlike to windows) with PyO3 and Maturin

I'm wondering if anyone has experience with cross-compiling to windows with pyo3 and maturin. The pyo3 docs say: Cross compiling PyO3 modules is relatively straightforward and requires a few pieces of software: A toolchain for your target. The…
Jasper
  • 1,193
  • 1
  • 9
  • 14
4
votes
0 answers

How to build modules hierarchy with pyo3?

Trying to build modules hierarchy with pyo3 and this code pub mod types; pub mod sources; use pyo3::prelude::*; use pyo3::wrap_pymodule; use sources::file::{find_days, read_many, read_one}; #[pymodule] fn file(_py: Python, m: &PyModule) ->…
cvb
  • 337
  • 2
  • 8
4
votes
2 answers

How can I implement python operators in PyO3

I'm trying to implement a vector class in rust for my math library. #[pyclass] struct Vec2d { #[pyo3(get, set)] x: f64, #[pyo3(get, set)] y: f64 } But I can't figure out how I can overload the standard operators (+, -, *, /) I…
N0name
  • 133
  • 1
  • 7
3
votes
1 answer

How to overwrite method in Python used in another method in Rust?

I have a struct with two methods return_modified_value and modify_value in Rust. return_modified_value uses modify_value. Rust implementation of modify_value doesn't matter because I want to implement this method in class that will inherit after my…
3
votes
2 answers

Implementing decorators in terms of closures with pyo3

As a learning exercise I'm trying to implement a parameterised decorator function in pyo3 using closures. The pyo3 documentation contains an example of a (non-parameterised) decorator implemented as a class with a __call__ method, and I've built on…
virgesmith
  • 762
  • 1
  • 7
  • 18
3
votes
1 answer

Generic function argument that accepts functions that implement a macro

I'm using pyo3 to add some rust to my python project (for performance), and wanted to create a function to make adding submodules easier. My current code: fn register_submodule(name: &str, python: Python, parent: &PyModule, funcs: Vec) ->…
0x5DA
  • 77
  • 1
  • 5
3
votes
1 answer

Async function with self-reference in Pyo3

I have the following example: use pyo3::prelude::*; use std::collections::{HashMap, HashSet}; use std::sync::RwLock; #[pyclass] struct Rustex { map: RwLock>, contexts: RwLock>, } #[pymethods] impl…
Alex
  • 439
  • 5
  • 16
1
2
3
10 11