INNER CODE UNIT · Rust

init

stjude-rust-labs/sprocket · crates/sprocket-py/src/lib.rs:495

    fn init(module: &Bound<'_, PyModule>) -> PyResult<()> {
        /// Recursively visits every submodule in this Python extension and adds
        /// it to `sys.modules`.
        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}");

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…