Crates.io | pyo3-commonize |
lib.rs | pyo3-commonize |
version | 0.1.4 |
source | src |
created_at | 2024-08-26 17:06:41.846346 |
updated_at | 2024-09-13 09:13:16.894858 |
description | Allow PyO3 classes to be passed between different PyO3 crates |
homepage | |
repository | https://github.com/yasuo-ozu/pyo3-commonize |
max_upload_size | |
id | 1352432 |
size | 18,599 |
Using this crate, the classes defined using #[pyclass] can be passed between multiple Python native extensions built with PyO3.
When using this crate, it is essential to pay strict attention to ABI compatibility. To define ABI-compatible classes, it is recommended to use crates like abi_stable. (In the future, this crate may require the implementation of abi_stable::StableAbi
.)
To mitigate this issue, pyo3-commonize checks the following points: the last modified date of the source code for the specified crate and all of its dependency crates, as well as the build environment (target and host versions, Rust flags, optimization levels, etc.).
Example to pass MyClass
class defined in acceptor
crate from donor
to acceptor
.
acceptor
crateCargo.toml
[package]
name = "acceptor"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["rlib", "cdylib"]
[dependencies]
pyo3-commonize = "0.1.0"
pyo3 = "*" # pyo3-commonize fixes pyo3 version
pyproject.toml
[build-system]
requires = ["poetry-core>=1.0.0", "maturin>=1.0,<2.0"]
build-backend = "maturin"
[project]
name = "acceptor"
version = "0.1.0"
license = { text = "MIT" }
src/lib.rs
use pyo3::prelude::*;
use pyo3_commonize::{commonize, Commonized};
#[pyclass]
#[derive(Commonized)]
pub struct MyClass;
#[pyfunction]
fn accept(_my_class: Py<MyClass>) {}
#[pymodule]
fn acceptor(py: Python<'_>, m: Bound<'_, PyModule>) -> PyResult<()> {
// This function should be called at first
commonize::<MyClass>(py)?;
m.add_function(wrap_pyfunction!(accept, &m)?)?;
Ok(())
}
donor
crateCargo.toml
[package]
name = "donor"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["rlib", "cdylib"]
[dependencies]
pyo3-commonize = "0.1.0"
pyo3 = "*" # pyo3-commonize fixes pyo3 version
acceptor = { path = "<path>" }
pyproject.toml
[build-system]
requires = ["poetry-core>=1.0.0", "maturin>=1.0,<2.0"]
build-backend = "maturin"
[project]
name = "donor"
version = "0.1.0"
license = { text = "MIT" }
src/lib.rs
use acceptor::MyClass;
use pyo3::prelude::*;
use pyo3_commonize::commonize;
#[pyfunction]
fn generate() -> MyClass { MyClass }
#[pymodule]
fn donor(py: Python<'_>, m: Bound<'_, PyModule>) -> PyResult<()> {
commonize::<MyClass>(py)?;
m.add_function(wrap_pyfunction!(generate, &m)?)?;
Ok(())
}