2022-05-17 02:50:51 +00:00
|
|
|
#![cfg(all(feature = "macros", not(PyPy)))]
|
2024-02-24 13:50:18 +00:00
|
|
|
|
2022-05-17 02:50:51 +00:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn foo() -> usize {
|
|
|
|
123
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
2024-02-28 22:36:50 +00:00
|
|
|
fn module_fn_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
|
m.add_function(wrap_pyfunction!(foo, m)?)?;
|
2022-05-17 02:50:51 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-24 13:50:18 +00:00
|
|
|
#[pymodule]
|
|
|
|
mod module_mod_with_functions {
|
|
|
|
#[pymodule_export]
|
|
|
|
use super::foo;
|
|
|
|
}
|
|
|
|
|
2022-05-17 02:50:51 +00:00
|
|
|
#[cfg(not(PyPy))]
|
|
|
|
#[test]
|
|
|
|
fn test_module_append_to_inittab() {
|
|
|
|
use pyo3::append_to_inittab;
|
2024-02-24 13:50:18 +00:00
|
|
|
|
|
|
|
append_to_inittab!(module_fn_with_functions);
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-05-17 02:50:51 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-05 18:44:00 +00:00
|
|
|
py.run_bound(
|
2022-05-17 02:50:51 +00:00
|
|
|
r#"
|
2024-02-24 13:50:18 +00:00
|
|
|
import module_mod_with_functions
|
|
|
|
assert module_mod_with_functions.foo() == 123
|
2022-05-17 02:50:51 +00:00
|
|
|
"#,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2023-07-21 13:30:02 +00:00
|
|
|
.map_err(|e| e.display(py))
|
2022-05-17 02:50:51 +00:00
|
|
|
.unwrap();
|
2024-02-24 13:50:18 +00:00
|
|
|
});
|
2022-05-17 02:50:51 +00:00
|
|
|
}
|