e0e3981e17
* #[pymodule] mod some_module { ... } v3 Based on #2367 and #3294 Allows to export classes, native classes, functions and submodules and provide an init function See test/test_module.rs for an example Future work: - update examples, README and guide - investigate having #[pyclass] and #[pyfunction] directly in the #[pymodule] Co-authored-by: David Hewitt <mail@davidhewitt.dev> Co-authored-by: Georg Brandl <georg@python.org> * tests: group exported imports * Consolidate pymodule macro code to avoid duplicates * Makes pymodule_init take Bound<'_, PyModule> * Renames #[pyo3] to #[pymodule_export] * Gates #[pymodule] mod behind the experimental-declarative-modules feature * Properly fails on functions inside of declarative modules --------- Co-authored-by: David Hewitt <mail@davidhewitt.dev> Co-authored-by: Georg Brandl <georg@python.org>
60 lines
1.2 KiB
Rust
60 lines
1.2 KiB
Rust
#![cfg(all(feature = "macros", not(PyPy)))]
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
#[pyfunction]
|
|
fn foo() -> usize {
|
|
123
|
|
}
|
|
|
|
#[pymodule]
|
|
fn module_fn_with_functions(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
|
m.add_function(wrap_pyfunction!(foo, m)?).unwrap();
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(feature = "experimental-declarative-modules")]
|
|
#[pymodule]
|
|
mod module_mod_with_functions {
|
|
#[pymodule_export]
|
|
use super::foo;
|
|
}
|
|
|
|
#[cfg(not(PyPy))]
|
|
#[test]
|
|
fn test_module_append_to_inittab() {
|
|
use pyo3::append_to_inittab;
|
|
|
|
append_to_inittab!(module_fn_with_functions);
|
|
|
|
#[cfg(feature = "experimental-declarative-modules")]
|
|
append_to_inittab!(module_mod_with_functions);
|
|
|
|
Python::with_gil(|py| {
|
|
py.run_bound(
|
|
r#"
|
|
import module_fn_with_functions
|
|
assert module_fn_with_functions.foo() == 123
|
|
"#,
|
|
None,
|
|
None,
|
|
)
|
|
.map_err(|e| e.display(py))
|
|
.unwrap();
|
|
});
|
|
|
|
#[cfg(feature = "experimental-declarative-modules")]
|
|
Python::with_gil(|py| {
|
|
py.run_bound(
|
|
r#"
|
|
import module_mod_with_functions
|
|
assert module_mod_with_functions.foo() == 123
|
|
"#,
|
|
None,
|
|
None,
|
|
)
|
|
.map_err(|e| e.display(py))
|
|
.unwrap();
|
|
});
|
|
}
|