Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

60 questions
2
votes
1 answer

PyO3 - Deriving FromPyObject for Enums

I'm trying to build a Python package from Rust using PyO3 (version: 0.13.2). Right now I'm stuck trying to get the conversion working for enums. I have a simple enum like so: #[derive(FromPyObject)] #[derive(Copy,Clone,PartialEq,Eq)] enum Direction…
xjvr
  • 23
  • 4
2
votes
1 answer

Pass list of lists as argument to Rust from Python using PyO3

I'm trying to pass a list of lists from Python to Rust using Py03. The function I'm trying to pass it to has this signature: pub fn k_nearest_neighbours(k: usize, x: &[[f32; 2]], y: &[[f32; 3]]) -> Vec> I'm writing bindings for a…
Casper S
  • 78
  • 7
2
votes
0 answers

How can I make a Rust struct that modifies its own value in a different thread?

I'm making a Python module, written in Rust, using pyo3, that will: Run its own thread Read an input pin on a Raspberry Pi, counting how many times the state changes Let Python query the counter So far, my code looks like this: use…
John
  • 1,257
  • 11
  • 28
2
votes
1 answer

Rust Numpy library - iterate by rows: unable to build a NpySingleIterBuilder::readwrite that returns rows instead of single values

I am trying to iterate by rows over a Numpy Array. The array is accessed thru PyO3 and I think the library acess to the underlying C object just fine but I can't seem to find the reference for a more complex SingleIteratorBuilder that helps me…
2
votes
1 answer

TypeError in Python using PyAny in reflected numeric emulator (e.g. __radd__) for a Rust pyo3 pyclass struct

I've created a Rust library for python using pyo3. This contains a pyclass struct that implements several of PyNumberProtocol methods like __add__, __sub__, etc... to allow python operators like + and - to work on the class. I'm using PyAny as the…
Jules SJ
  • 134
  • 5
2
votes
1 answer

PyO3 - passing class extending another class as a function argument

I have a library in Rust and I want to be able to use it from Python. So I can do the bindings using PyO3. Let's suppose that I have a following library in Rust: pub trait Animal { fn make_sound(); } pub struct Dog {} impl Animal for Dog { …
Damian
  • 116
  • 9
2
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,148
  • 2
  • 11
  • 22
2
votes
1 answer

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,153
  • 1
  • 9
  • 13
2
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
  • 21
  • 1
2
votes
1 answer

Garbage collection for vector of object in PyO3

I have 2 pyclasses Block and BlockGroup. #[pyclass] struct Block { start: i32, stop: i32, } #[pyclass] struct BlockGroup { blocks: Vec } I'm new to PyO3 and I have read the documentation about garbage collection but I don't…
kentwait
  • 1,643
  • 2
  • 19
  • 38
1
vote
1 answer

How to call Python async function from Rust?

I have read this answer(How to call Rust async method from Python?) and what I want to do is the opposite of this. I want to call a Python async function and await it in tokio's runtime. Is it possible? I have given it some tries but I am facing an…
Sanskar Jethi
  • 362
  • 2
  • 8
1
vote
1 answer

How to pass an async function as a parameter in Rust PyO3

When we write vanilla rust and we have to pass in an async function as an argument to another function we do the following: pub f( test: &dyn Fn(&'a mut String, String, String, TcpStream) -> F, ) where F: Future + 'a, But…
Sanskar Jethi
  • 362
  • 2
  • 8
1
vote
0 answers

Rust pyo3: Any way to pass argument to pyfunction by reference?

fn gen_data<'py> (py: Python<'py>, a: Vec, b: Vec) -> PyResult<&'py PyBytes>{ let dp = MyDataStruct { data: Bytes::from(a), extra_info: Bytes::from(b), }; Ok(PyBytes::new(py,…
Li haonan
  • 490
  • 1
  • 4
  • 22
1
vote
0 answers

How to make a Python "rust-Extension" module that behaves exactly like C-Extensions in terms of call overhead and processing speed?

The closer option I have found is pyo3, but it isn't clear to me if it adds any extra overhead when compared to the traditional C-extensions. From here it seems like such C-extension behavior is possible through borrowed objects (I still have to…
olyk
  • 409
  • 3
  • 10
1
vote
1 answer

How can I convert PyArray into Vec> in Rust

I am using the numpy crate in Rust to work with 2D arrays that come from python. PyArray (https://docs.rs/numpy/0.11.0/numpy/array/struct.PyArray.html) implements a from_vec2() function, which converts a Vec> into a PyArray (2D…
Yaxlat
  • 415
  • 1
  • 6
  • 18