Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

161 questions
1
vote
0 answers

pyo3: Passing a reference of Rust's Self into a method of Self with attribute `#[classmethod]`

Context of my question: In pydantic's, you can define a CustomType, which is any python class with the following class method named __get_validators__(cls) defined. The objective is to have pydantic validate the input to the initialization of the…
Jim
  • 450
  • 2
  • 10
1
vote
0 answers

Can a Python native extension build and return a dict without holding the GIL the whole time?

Say I want to implement a native extension for deserializing a JSON object from a file. For an object with many keys, the first part of the execution time will be I/O, and the second part will be filling the dict up. I know it's safe to release…
bplevin
  • 36
  • 3
1
vote
1 answer

Using Box with PyO3

I have a struct in Rust that works like a linked-list that I want to expose to Python. The struct has a parent field, which is a reference to a parent which is a struct of the same type. I need to wrap this in a Box, since Rust complains about…
David Chanin
  • 533
  • 6
  • 17
1
vote
0 answers

How should I communicate bytes to Python using pyo3?

I am new to Rust, and I am writing a library that creates background threads that listen and handle TCP communications. I want to store the latest n bytes for every TCP client and have Python to be able to fetch them. The way I am thinking about…
1
vote
0 answers

How to expose entire Rust structure to Python at once

I'm a Rust newbie exploring language features with the possible result of recommending Rust as a replacement for C/C++ for our next FW iteration. One important topic is Python interop for unit testing and modeling. I've got this far exposing a Rust…
Andrew Voelkel
  • 513
  • 4
  • 10
1
vote
1 answer

maturin build not working with older python versions

I created anaconda environment $ python --version Python 3.7.13 $ pip --version pip 22.2.2 from C:\tools\miniconda3\envs\py37\lib\site-packages\pip (python 3.7) In pyproject.toml I have requires-python = ">=3.7" [build-system] requires =…
alagris
  • 1,838
  • 16
  • 31
1
vote
2 answers

Using Pylint with PyModule generated using PyO3 and maturin

Pylint will not recognize any of the functions from a PyModule that I created using PyO3 and maturin. All of the functions import and run fine in the python code base, but for some reason Pylint is throwing E1011: no-member warnings. Below is a…
codeAndStuff
  • 507
  • 6
  • 19
1
vote
2 answers

Rust string comparison same speed as Python . Want to parallelize the program

I am new to rust. I want to write a function which later can be imported into Python as a module using the pyo3 crate. Below is the Python implementation of the function I want to implement in Rust: def pcompare(a, b): letters = [] for i,…
1
vote
1 answer

Rust pyo3 function is faster in python when ran multiple times instead of once

I implemented an algorithm in rust for speed, which is then built into a python module. Running the function is indeed much faster than the python implementation. But I noticed an interesting quirk: Running the function a lot of times (say, 1…
DaNubCoding
  • 320
  • 2
  • 11
1
vote
1 answer

pyo3 optionally generate python bindings for rust struct

I have defined a few structs in my code and if a certain feature is enabled on the crate, I would like to generate Python bindings for those structs as well. Right now I am not able to get it correctly. Let's say I have a struct MyStruct for which I…
gabhijit
  • 3,345
  • 2
  • 23
  • 36
1
vote
1 answer

pyo3 rust module kills Python on import

I'm writing my first Rust-based Python module, and it kills the Python process on import. I've got it down to a pretty minimal example, based loosely on the html-py-ever example (which does run for me without crashing). I'm running Python 3.8 on an…
Jeremy McGibbon
  • 3,527
  • 14
  • 22
1
vote
0 answers

How to use Python mehtod interactive with Rust?

I'm working with some code that need to execute python method in Rust with Pyo3, but I have some problem in sahring a Struct between rust and python. pub struct Wallet { pub cash: i64 } impl Wallet { pub fn get_money(&mut self, num: i64) { …
Hakase
  • 211
  • 1
  • 12
1
vote
1 answer

How to wrap function with NDArray input and output with PyO3?

I want to wrap a function that takes an one-dimensional NDArray (rust-numpy) and an usize as parameters, and returns a one-dimensional array using PyO3 to call the code from python. Unfortunately, I can't find a good example of how to deal with the…
0xSingularity
  • 577
  • 6
  • 36
1
vote
0 answers

How to improve a PyO3 interface to a generic struct?

I've created a Rust library to use from Python to do some heavy processing. It works fairly well but I'm left with code that feels bloated and hard to maintain, so I figure there must be a better way. I've tried a few different options using a…
Jules SJ
  • 430
  • 2
  • 8
1
vote
1 answer

How to change a value so that it doesn't have static lifetime in Rust

I have the following function that uses PyO3 to call a python function and get a result (in this case, an int that gets assigned to a i32): fn run_python<'a, T: FromPyObject<'a> + Clone>(func_name: &str) -> Result { Python::with_gil(|py|…
Eli Stevens
  • 1,447
  • 1
  • 12
  • 21