blob: 4c28efae2e5ac5f113ca28fa94f765e986dcf647 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#![allow(unused)]
fn main() {
use pyo3::prelude::*;
#[pymodule]
fn parent_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
register_child_module(py, m)?;
Ok(())
}
fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> {
let child_module = PyModule::new(py, "child_module")?;
child_module.add_function(wrap_pyfunction!(func, child_module)?)?;
parent_module.add_submodule(child_module)?;
Ok(())
}
#[pyfunction]
fn func() -> String {
"func".to_string()
}
Python::with_gil(|py| {
use pyo3::types::IntoPyDict;
use pyo3::wrap_pymodule;
let parent_module = wrap_pymodule!(parent_module)(py);
let ctx = [("parent_module", parent_module)].into_py_dict(py);
py.run(
"assert parent_module.child_module.func() == 'func'",
None,
Some(&ctx),
)
.unwrap();
})
}
|