INNER CODE UNIT · Rust
register_submodules
stjude-rust-labs/sprocket · crates/sprocket-py/src/lib.rs:498
fn register_submodules(
module: &Bound<'_, PyModule>,
parent_name: &str,
sys_modules: &Bound<'_, PyAny>,
) -> PyResult<()> {
// Loop through the names of all items in the module.
for item_name in module.index()? {
// Cast name from `PyAny` to `PyString`.
let item_name: &Bound<'_, PyString> = item_name.cast()?;
// Get the actual item from its name.
let item = module.getattr(item_name)?;
// If the item is a submodule...
if let Ok(submodule) = item.cast::<PyModule>() {
let submodule_name = format!("{parent_name}.{item_name}");
// ...add the submodule to `sys.modules`.
sys_modules.set_item(&submodule_name, submodule)?;