##// END OF EJS Templates
rust-pyo3: dagop submodule implementation...
rust-pyo3: dagop submodule implementation This is the first demonstration that the passing of objects through the Python interpreter from `rustext` to `pyo3_rustext` actually works. In the Python tests, we conflate the presence of the `pyo3_rustext` package with that of `rustext`, as we do not plan to support building one and not the other (we hope to convert fully to PyO3 soon). The skipping is actually done by the base test class. The implementation of `rank` is as trivial as it was in `hg-cpython`, yet it required to be able to convert exceptions from `vcsgraph`.

File last commit:

r53304:c5128c54 default
r53310:20fe0bf9 default
Show More
util.rs
28 lines | 1.2 KiB | application/rls-services+xml | RustLexer
use pyo3::prelude::*;
use pyo3::types::PyDict;
/// Create the module, with `__package__` given from parent
///
/// According to PyO3 documentation, which links to
/// <https://github.com/PyO3/pyo3/issues/1517>, the same convoluted
/// write to sys.modules has to be made as with the `cpython` crate.
pub(crate) fn new_submodule<'py>(
py: Python<'py>,
package_name: &str,
name: &str,
) -> PyResult<Bound<'py, PyModule>> {
let dotted_name = &format!("{}.{}", package_name, name);
let m = PyModule::new(py, name)?;
m.add("__package__", package_name)?;
m.add("__doc__", "DAG operations - Rust implementation")?;
let sys = PyModule::import(py, "sys")?;
// according to the doc, we could make a static PyString out of
// "modules" with the `intern!` macro, but this is used only at
// registration so it may not be worth the effort.
let sys_modules: Bound<'_, PyDict> = sys.getattr("modules")?.extract()?;
sys_modules.set_item(dotted_name, &m)?;
// Example C code (see pyexpat.c and import.c) will "give away the
// reference", but we won't because it will be consumed once the
// Rust PyObject is dropped.
Ok(m)
}